0

This is what I have...

[{
  "programName": "Testing 102",
  "recommendedComparisons": [{
    "programName": "Testing 101",
    "id": 2
  }, {
    "programName": "Testing 401",
    "id": 4
  }],
  "id": 1,
  "programId": 3
}, {
  "programName": "Testing 101",
  "recommendedComparisons": [{
    "programName": "Testing 102",
    "id": 3
  }],
  "id": 9,
  "programId": 2
}]

What I want is two lists of recommendedComparisons.

Testing 101, Testing 401 and Testing 102

I'm sure there must be simple way.

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32

4 Answers4

0
var data = [{
  "programName": "Testing 102",
  "recommendedComparisons": [{
    "programName": "Testing 101",
    "id": 2
  }, {
    "programName": "Testing 401",
    "id": 4
  }],
  "id": 1,
  "programId": 3
}, {
  "programName": "Testing 101",
  "recommendedComparisons": [{
    "programName": "Testing 102",
    "id": 3
  }],
  "id": 9,
  "programId": 2
}];

var result = data.map(function(item) {
    return item.recommendedComparisons.map(function(comp) {
        return comp.programName;
    });
});
abigperson
  • 5,252
  • 3
  • 22
  • 25
0

What you are looking for is the Array object's map function.

You are looking for just the program names of the recommended comparisons. For that, you would need 1 map function to get the comparisons, then you would need to call map again, and inside that function call map on the subarrays to return the program names.

Since you mentioned comma-separated-lists, it would be better to not return the array itself. Instead create a new string, and join the subarrays with a comma, appending it with a new line.

I believe this is what you are looking for

var array = [{"programName":"Testing 102","recommendedComparisons":[{"programName":"Testing 101","id":2},{"programName":"Testing 401","id":4}],"id":1,"programId":3},{"programName":"Testing 101","recommendedComparisons":[{"programName":"Testing 102","id":3}],"id":9,"programId":2}]

var results = array.map(function(a) { return a.recommendedComparisons
  }).map(function(b) {
      return b.map(function(c) {
          return c.programName;
      });
  });

var str = "";

for (var i = 0; i < results.length; i++) {
    str += results[i].join(',') + "\n"
}

console.log(str);
Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
0
  1. Making use of the map function allows to transform the object.

    var recommendedComparisons = data.map(x => x.recommendedComparisons.map(x=>x.programName));

  2. Using Destructuring to get 2 list of results.

    var [firstList,secondList] = recommendedComparisons;

var data = [{
  "programName": "Testing 102",
  "recommendedComparisons": [{
    "programName": "Testing 101",
    "id": 2
  }, {
    "programName": "Testing 401",
    "id": 4
  }],
  "id": 1,
  "programId": 3
}, {
  "programName": "Testing 101",
  "recommendedComparisons": [{
    "programName": "Testing 102",
    "id": 3
  }],
  "id": 9,
  "programId": 2
}];

var recommendedComparisons = data.map(x => x.recommendedComparisons.map(x=>x.programName));

var [firstList,secondList] = recommendedComparisons;
console.log(firstList);
console.log(secondList);
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
0

I haven't been able to test this out yet but I'm pretty sure this is right. I'll test it now.

const list = [
  {
    "programName":"Testing 102",
    "recommendedComparisons":[
      { "programName": "Testing 101", "id":2 },
      { "programName": "Testing 401", "id":4 }
    ],
    "id":1,
    "programId":3
  },
  {
    "programName":"Testing 101",
    "recommendedComparisons":[
       {"programName":"Testing 102","id":3}
    ],
    "id":9,
    "programId":2
  }
];

let listOfComparisonLists = [];

list.map((program) => {
  const programNames = program.recommendedComparisons.map((comp) => comp.programName);

  listOfComparisonLists.push(programNames);
});

console.log(listOfComparisonLists); // [['Testing 104', 'Testing 401'],['Testing 102']]