如何使用ES6从数组中嵌套在另一个对象中的数组中获取对象

时间:2019-01-22 17:10:09

标签: javascript arrays ecmascript-6 ecmascript-2017

我的数据具有以下结构:

var DATA = {
'device_groups': [{
  'id': '1',
  'name': 'group 1',
  'devices': [{
    'id': 11,
    'name': 'device 11',
    'active': 1
  }, {
    'id': 12,
    'name': 'device 12',
    'active': 0
  }, {
    'id': 13,
    'name': 'device 13',
    'active': 0
  }] 
}, {
  'id': '2',
  'name': 'group 2',
  'devices': [{
    'id': 21,
    'name': 'device 21',
    'active': 1
  }, {
    'id': 22,
    'name': 'device 22',
    'active': 0
  }, {
    'id': 23,
    'name': 'device 23',
    'active': 1
  }]
}, {
  'id': '3',
  'name': 'group 3',
  'devices': [{
    'id': 31,
    'name': 'device 31',
    'active': 1
  }, {
    'id': 32,
    'name': 'device 32',
    'active': 0
  }, {
    'id': 33,
    'name': 'device 33',
    'active': 1
  }]  
}]
};

从所有这些“ device_groups”数组和内部“ devices”数组中,我需要获得一个对象数组,其中“ active”为true(1)。

如何使用ES6 +方法?

5 个答案:

答案 0 :(得分:1)

您可以像这样使用mapfilter

var DATA={'device_groups':[{'id':'1','name':'group 1','devices':[{'id':11,'name':'device 11','active':1},{'id':12,'name':'device 12','active':0},{'id':13,'name':'device 13','active':0}]},{'id':'2','name':'group 2','devices':[{'id':21,'name':'device 21','active':1},{'id':22,'name':'device 22','active':0},{'id':23,'name':'device 23','active':1}]},{'id':'3','name':'group 3','devices':[{'id':31,'name':'device 31','active':1},{'id':32,'name':'device 32','active':0},{'id':33,'name':'device 33','active':1}]}]}

const filtered = DATA.device_groups.map(a => a.devices.filter(a => a.active === 1)),
  output = [].concat(...filtered);

console.log(output)

或者使用简单的reduce

var DATA={'device_groups':[{'id':'1','name':'group 1','devices':[{'id':11,'name':'device 11','active':1},{'id':12,'name':'device 12','active':0},{'id':13,'name':'device 13','active':0}]},{'id':'2','name':'group 2','devices':[{'id':21,'name':'device 21','active':1},{'id':22,'name':'device 22','active':0},{'id':23,'name':'device 23','active':1}]},{'id':'3','name':'group 3','devices':[{'id':31,'name':'device 31','active':1},{'id':32,'name':'device 32','active':0},{'id':33,'name':'device 33','active':1}]}]}

const devices = DATA.device_groups
                  .reduce((a,d) => a.concat(d.devices.filter(f => f.active)),[]);

console.log(devices)

答案 1 :(得分:0)

您可以使用reducefilter进行操作。

通过过滤器,我们仅从devices中提取状态为active的元素,然后将它们合并到final output

var DATA={'device_groups':[{'id':'1','name':'group 1','devices':[{'id':11,'name':'device 11','active':1},{'id':12,'name':'device 12','active':0},{'id':13,'name':'device 13','active':0}]},{'id':'2','name':'group 2','devices':[{'id':21,'name':'device 21','active':1},{'id':22,'name':'device 22','active':0},{'id':23,'name':'device 23','active':1}]},{'id':'3','name':'group 3','devices':[{'id':31,'name':'device 31','active':1},{'id':32,'name':'device 32','active':0},{'id':33,'name':'device 33','active':1}]}]}

let output = DATA.device_groups.reduce((op,cur)=>{
  let temp = cur.devices.filter(ele=> ele.active)
  op = op.concat(temp)
  return op;
},[])

console.log(output)

答案 2 :(得分:0)

基本上,您可以使用reducefilter来实现所需的目标

var DATA = {
  'device_groups': [{
    'id': '1',
    'name': 'group 1',
    'devices': [{
      'id': 11,
      'name': 'device 11',
      'active': 1
    }, {
      'id': 12,
      'name': 'device 12',
      'active': 0
    }, {
      'id': 13,
      'name': 'device 13',
      'active': 0
    }]
  }, {
    'id': '2',
    'name': 'group 2',
    'devices': [{
      'id': 21,
      'name': 'device 21',
      'active': 1
    }, {
      'id': 22,
      'name': 'device 22',
      'active': 0
    }, {
      'id': 23,
      'name': 'device 23',
      'active': 1
    }]
  }, {
    'id': '3',
    'name': 'group 3',
    'devices': [{
      'id': 31,
      'name': 'device 31',
      'active': 1
    }, {
      'id': 32,
      'name': 'device 32',
      'active': 0
    }, {
      'id': 33,
      'name': 'device 33',
      'active': 1
    }]
  }]
};
const deviceGroups = DATA.device_groups;

const solution = deviceGroups.reduce((result, devicegroup) => {
  const filteredDevices = devicegroup.devices.filter(device => device.active === 1)
  return [...result, ...filteredDevices]
}, [])

console.log(solution)

答案 3 :(得分:0)

使用reduce将来自每个devices的{​​{1}}的内部数组合并为device_groups的一个数组。然后使用devices过滤合并的阵列,以得到一个活动的设备的单个阵列。

active === 1

答案 4 :(得分:0)

您可以仅使用forEach并传播...运算符,并在最终数组中将active属性为true的情况下过滤结果

var DATA = {'device_groups': [{'id': '1','name': 'group 1','devices': [{'id': 11,'name': 'device 11','active': 1}, {'id': 12,'name': 'device 12','active': 0}, {'id': 13,'name': 'device 13','active': 0}] }, {'id': '2','name': 'group 2','devices': [{'id': 21,'name': 'device 21','active': 1}, {'id': 22,'name': 'device 22','active': 0}, {'id': 23,'name': 'device 23','active': 1
  }]}, {'id': '3','name': 'group 3','devices': [{'id': 31,'name': 'device 31','active': 1}, {'id': 32,'name': 'device 32','active': 0}, {'id': 33,'name': 'device 33','active': 1}]}]};

const result =[];
DATA.device_groups.forEach(devGroup => result.push(...devGroup.devices.filter(d => d.active)));
console.log(result);