如何将数组的嵌套数组转换为逗号分隔的字符串

how to convert nested array of arrays into comma separated string

本文关键字:数组 字符串 分隔 嵌套 转换      更新时间:2023-09-26

我有一个包含2个嵌套对象的对象。它们都是数组的数组。我需要将两者的值连接成一个逗号分隔的字符串。Javascript、jquery或linqjs都可以。我有了一个开始,但我卡住了。这是一个活塞恰好

我需要获取数据和产品值并将它们连接起来。最终结果应该像这样

newString = " product - 1,3500, 2000 | product - 2,5400, 1800 |等."

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(" | "));

请查看:Plunker

var newString = [];
var modifiedNames = userDefinedSeries.product.map(function(product, index) {
  var productString = product.map(function (p) {
    return p.replace(" ", '-');
  }).join(' ');
  newString.push(productString + ', ' + userDefinedSeries.data[index].join(', '));
});
console.log(newString.join(' | '));
    var newString = "";
    for(var i=0; i< userDefinedSeries.uid.length;i++){
        newString += userDefinedSeries.product[i];
        newString += ","+userDefinedSeries.data[i];
        newString += "|";
    }
    console.log(newString);// your required string