如何获取数组的所有数组对象

时间:2018-06-06 15:12:34

标签: javascript ecmascript-6

数组对象中有一个目标数组:

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

我需要将所有目标对象作为一个数组:

需要的结果

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

我尝试使用map()

array.map(item => item.target)

但是这会产生一个嵌套数组,如:[ [ { _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)
)

6 个答案:

答案 0 :(得分:3)

您可以使用concat加入数组。使用扩展运算符和map循环遍历数组。

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

答案 1 :(得分: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);

答案 2 :(得分:0)

您可以使用reduce()。它循环遍历一个数组(类似于map()),但随着时间的推移创建一个新的结果。

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

或者,如果您无法使用spreads,则可以改为使用concat()

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

答案 3 :(得分: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);




答案 4 :(得分:0)

您可以使用reduce方法(Array.prototype.reduce

此答案在连接之前检查属性target

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

答案 5 :(得分: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)

相关问题