我有一个包含 2 个嵌套对象的对象。它们都是数组的数组。我需要将两者的值连接成逗号分隔的字符串。 Javascript、jquery 或 linqjs 都可以。我开始了,但我被困住了。这是一个笨蛋 plunker
我需要获取数据和产品值(value)并加入它们。最终结果需要像这样
newString = "Product-1, 3500, 2000 | Product-2, 5400, 1800 | etc.."
json对象
var userDefinedSeries = {"style":"normal","color":"rgb(0, 0, 255)","width":4,"uid":[["425780c9-9727-4c5d-9bc4-65ce3334b0aa"],["06a8a24e-6a59-43e0-89a4-9fe4db55cac5"],["e1c73a33-ba2c-4d8d-9751-3c336442da84"]],"data":[[2500,50000],[2500,40000],[3000,40000]],"product":[["Product 3"],["Product 1"],["Product 2"]],"name":"Subject Property","type":"scatterLine","$$hashKey":"object:91"};
我在做什么
var newString = [];
var string;
var modifiedNames = userDefinedSeries.data.map(function(arrayCell) {
for (var key in userDefinedSeries.data){
for (var keyP in userDefinedSeries.product){
string = arrayCell[0] + " , " + arrayCell[1] + "|";
}
}
newString.push(string);
return string;
});
console.log(newArray);
最佳答案
使用下面的代码我得到了这个输出:
Product-3,2500,50000 | Product-1,2500,40000 | Product-2,3000,40000
var userDefinedSeries = {
"style":"normal",
"color":"rgb(0, 0, 255)",
"width":4,
"uid":[["425780c9-9727-4c5d-9bc4-65ce3334b0aa"],["06a8a24e-6a59-43e0-89a4-9fe4db55cac5"],["e1c73a33-ba2c-4d8d-9751-3c336442da84"]],
"data":[[2500,50000],[2500,40000],[3000,40000]],
"product":[["Product 3"],["Product 1"],["Product 2"]],
"name":"Subject Property","type":"scatterLine","$$hashKey":"object:91"};
var newString = [];
var string;
var key = 0;
userDefinedSeries.product.map(function(arrayCell) {
string = arrayCell + "," + userDefinedSeries.data[key];
string = string.replace(" ","-");
newString.push(string);
key++;
});
console.log(newString.join(" | "));
关于javascript - 如何将嵌套数组转换为逗号分隔的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32469051/