0

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

1
  • If you have a library like underscore or lodash, you could use the flatmap function Commented Jun 6, 2018 at 15:16

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I don't know why someone downvoted this. This is a correct answer (see my similar one as well). You can use the ... operator instead of concat.
0

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

Comments

0

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

Comments

0

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)

Comments

0

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;
}, []);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.