Q:

extract array that is inside and array of objects

var objs = [
  {labels: ["label1", "label2"]}, 
  {labels: ["label1", "label3"]},
  {labels: ["label2", "label4"]} 
]

i am trying to extract this ["label1", "label2", "label3", "label4"]
labels = this.get('objs').map(function(obj) {
           return obj.labels.map(function(label) {
             return label;
           });
         });
console.log(labels);

But the above code printed [["label1", "label2"], ["label1", "label3"], ["label2", "label4"]]
Can someone help me with this?

A:

Firstly you could map your objs array to get just the labels array, then remove dupe entries with Set.

const objs = [
  {labels: ["label1", "label2"]}, 
  {labels: ["label1", "label3"]},
  {labels: ["label2", "label4"]},
];

const r = [...new Set([].concat(...objs.map(({ labels }) => labels)))];

console.log(r);

ES5:

const objs = [
  {labels: ["label1", "label2"]}, 
  {labels: ["label1", "label3"]},
  {labels: ["label2", "label4"]},
];

const r = [...new Set([].concat(...objs.map(function(v) {
  return v.labels;
})))];

console.log(r);

