如何从中获取所有approot
值并将其存储为简单数组?
office
输出应为:
var data = {
'XYZ': [{
office: 'xyz.in',
reportName: 'payroll',
event: 'open',
timestamp: '02-12-2019 00:15:29'
},
{
office: 'xyz.in',
reportName: 'payroll',
event: 'processed',
timestamp: '02-12-2019 00:15:32'
}
],
'yyy': [{
office: 'yyy.in',
reportName: 'payroll',
event: 'delivered',
timestamp: '02-12-2019 00:15:29'
},
{
office: 'yyy.in',
reportName: 'payroll',
event: 'open',
timestamp: '02-12-2019 00:15:32'
}
],
'zzz': [{
office: 'xyz.in',
reportName: 'payroll',
event: 'delivered',
timestamp: '02-12-2019 00:15:29'
},
{
office: 'xyz.in',
reportName: 'payroll',
event: 'open',
timestamp: '02-12-2019 00:15:32'
}
]
}
我的代码是
officesArray = ['xyz.in', 'yyy.in', 'xyz.in']
它打印var Office = (Object.entries(grpDatas).flatMap(([k, v]) => (v.forEach(({office})=> office))))
。
答案 0 :(得分:2)
使用map
代替forEach
,并且您正在访问email
而不是office
:
var data = {
'XYZ': [{
office: 'xyz.in',
reportName: 'payroll',
event: 'open',
timestamp: '02-12-2019 00:15:29'
},
{
office: 'xyz.in',
reportName: 'payroll',
event: 'processed',
timestamp: '02-12-2019 00:15:32'
}
],
'yyy': [{
office: 'yyy.in',
reportName: 'payroll',
event: 'delivered',
timestamp: '02-12-2019 00:15:29'
},
{
office: 'yyy.in',
reportName: 'payroll',
event: 'open',
timestamp: '02-12-2019 00:15:32'
}
],
'zzz': [{
office: 'xyz.in',
reportName: 'payroll',
event: 'delivered',
timestamp: '02-12-2019 00:15:29'
},
{
office: 'xyz.in',
reportName: 'payroll',
event: 'open',
timestamp: '02-12-2019 00:15:32'
}
]
}
var Email = Object.values(data).flatMap(item => [...new Set(item.map(({office})=> office))])
console.log(Email)
答案 1 :(得分:0)
您可以从所需属性中获得唯一值。
var data = { XYZ: [{ office: 'xyz.in', reportName: 'payroll', event: 'open', timestamp: '02-12-2019 00:15:29' }, { office: 'xyz.in', reportName: 'payroll', event: 'processed', timestamp: '02-12-2019 00:15:32' }], yyy: [{ office: 'yyy.in', reportName: 'payroll', event: 'delivered', timestamp: '02-12-2019 00:15:29' }, { office: 'yyy.in', reportName: 'payroll', event: 'open', timestamp: '02-12-2019 00:15:32' }], zzz: [{ office: 'xyz.in', reportName: 'payroll', event: 'delivered', timestamp: '02-12-2019 00:15:29' }, { office: 'xyz.in', reportName: 'payroll', event: 'open', timestamp: '02-12-2019 00:15:32' }] },
office = Object.values(data).flatMap(v => [...new Set(v.map(({ office }) => office))]);
console.log(office);
答案 2 :(得分:0)
const result = Object.keys(data)
.map( key => data[key])
.flat()
.map( element => element.office)
将所有内容转换为数组并将其展平有很大帮助!