user3142695
user3142695

Reputation: 17332

How to get all array objects of an array

There is an target array inside of an array object:

[ 
  { 
    _id: 'main1',
    target: [
      { _id: '1', foo: [bar] },
      { _id: '2', foo: [bar] }
    ]
  },
  { 
    _id: 'main2',
    target: [
      { _id: '3', foo: [bar] },
      { _id: '4', foo: [bar] }
    ]
  }
]

I need to get all target objects as one array:

Needed result

targets: [
  { _id: '1', foo: [bar] },
  { _id: '2', foo: [bar] }
  { _id: '3', foo: [bar] },
  { _id: '4', foo: [bar] }
]

I tried to use a map()

array.map(item => item.target)

But this results in a nested array like: [ [ { _id: '1', foo: [bar] } ] ]

var array = [ 
      { 
        _id: 'main1',
        target: [
          { _id: '1', foo: ["bar"] },
          { _id: '2', foo: ["bar"] }
        ]
      },
      { 
        _id: 'main2',
        target: [
          { _id: '3', foo: ["bar"] },
          { _id: '4', foo: ["bar"] }
        ]
      }
    ]

console.log(
  array.map(item => item.target)
)

Upvotes: 0

Views: 68

Answers (6)

Pablo Barría Urenda
Pablo Barría Urenda

Reputation: 5813

You can use reduce(). It loops through an Array (similarly to map()) but creating a new result as it goes.

array.reduce((result, current) => {
    result = [...result, ...current.target]
    return result;
}, []);

Or, if you cannot use spreads, you can use concat() instead:

array.reduce((result, current) => {
    result = result.concat(current.target);
    return result;
}, []);

Upvotes: 0

Shubhanu Sharma
Shubhanu Sharma

Reputation: 2132

var foo = [
  {
    "_id": "main1",
    "target": [
      {
        "_id": "1",
        "foo": "[bar]"
      },
      {
        "_id": "2",
        "foo": "[bar]"
      }
    ]
  },
  {
    "_id": "main2",
    "target": [
      {
        "_id": "3",
        "foo": "[bar]"
      },
      {
        "_id": "4",
        "foo": "[bar]"
      }
    ]
  }
]
var bar = [];
foo.map(function(val) {	
	bar = bar.concat(val.target);
})
console.log(bar)

Upvotes: 0

zfrisch
zfrisch

Reputation: 8660

You can use the reduce method ( Array.prototype.reduce )

This answer checks for the property target before concatenating.

let targets = obj_arr.reduce((a,c)=>c.target&&a.concat(c.target),[]);

let obj_arr = [{
    _id: 'main1',
    target: [{
        _id: '1',
        foo: [""]
      },
      {
        _id: '2',
        foo: [""]
      }
    ]
  },
  {
    _id: 'main2',
    target: [{
        _id: '3',
        foo: [""]
      },
      {
        _id: '4',
        foo: [""]
      }
    ]
  }
]

let targets = obj_arr.reduce((accumulator, current_value) =>
      current_value.target&&accumulator.concat(current_value.target), []);


console.log(targets);

Upvotes: 0

mscdeveloper
mscdeveloper

Reputation: 2889

var array = [
      {
        _id: 'main1',
        target: [
          { _id: '1', foo: ["bar"] },
          { _id: '2', foo: ["bar"] }
        ]
      },
      {
        _id: 'main2',
        target: [
          { _id: '3', foo: ["bar"] },
          { _id: '4', foo: ["bar"] }
        ]
      }
    ] ;

  var result=[];
  for(var key in array){
  	 target=array[key].target;
  	 for(var kt in target){
  	 	 result.push(target[kt]);
  	 }
  }

  console.log(result);

Upvotes: 0

amrender singh
amrender singh

Reputation: 8239

var arr = [ 
  { 
    _id: 'main1',
    target: [
      { _id: '1', foo: ["bar"] },
      { _id: '2', foo: ["bar"] }
    ]
  },
  { 
    _id: 'main2',
    target: [
      { _id: '3', foo: ["bar"] },
      { _id: '4', foo: ["bar"] }
    ]
  }
]
var result =  arr.reduce((a,o) => a.concat(o.target), []);
console.log(result);

Upvotes: 0

Eddie
Eddie

Reputation: 26844

You can use concat to join arrays. Use spread operator and map to loop thru the array.

let arr = [{_id:'main1',target:[{_id:'1',foo:['bar']},{_id:'2',foo:['bar']}]},{_id:'main2',target:[{_id:'3',foo:['bar']},{_id:'4',foo:['bar']}]}];
let result = [].concat(...arr.map(o => o.target));

console.log(result);

Upvotes: 3

Related Questions