简体   繁体   中英

how to convert nested array of arrays into comma separated string

I have a object with 2 nested objects. they are both a array of arrays. I need to join the values of both into a comma separated string. Javascript, jquery or linqjs will be fine. I got a start on it but I am stuck. here is a plunker plunker

I need to take the data and product values and join them. the end result needs to look like this

newString = "Product-1, 3500, 2000 | Product-2, 5400, 1800 | etc.."

json object

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"};

what i am working with

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

Please check: 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(' | '));

With the following code I got this output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM