我有一个对象数组,我使用groupBy获得了以下结果。
{ 'Cloud Revenue':
[ { sessionName: 'Business Growth',
channelName: 'Cloud Revenue',
channelId: '19:[email protected]',
startDate: '2020-04-12',
endDate: '2020-04-12',
startTime: '08:00:00.0000000',
endTime: '08:30:00.0000000' },
{ sessionName: 'Increase Market Value',
channelName: 'Cloud Revenue',
startDate: '2020-04-12',
endDate: '2020-04-12',
startTime: '06:30:00.0000000',
endTime: '07:00:00.0000000' } ],
General:
[ { sessionName: 'Value Proposition',
channelName: 'General',
startDate: '2020-04-12',
endDate: '2020-04-12',
startTime: '07:30:00.0000000',
endTime: '08:00:00.0000000' } ] }
现在,我需要按键枚举实体。例如,在上述情况下,我需要提取顶级属性名称(Cloud Revenue和General)并将其分配给字符串变量。我尝试使用foreach,_。map和_.each,没有任何东西可以为我提供顶级属性字符串。我该如何实现?
您可以将香草JS与Object.keys()
一起使用
const obj = {
'Cloud Revenue': [{
sessionName: 'Business Growth',
channelName: 'Cloud Revenue',
channelId: '19:[email protected]',
startDate: '2020-04-12',
endDate: '2020-04-12',
startTime: '08:00:00.0000000',
endTime: '08:30:00.0000000'
},
{
sessionName: 'Increase Market Value',
channelName: 'Cloud Revenue',
startDate: '2020-04-12',
endDate: '2020-04-12',
startTime: '06:30:00.0000000',
endTime: '07:00:00.0000000'
}
],
General: [{
sessionName: 'Value Proposition',
channelName: 'General',
startDate: '2020-04-12',
endDate: '2020-04-12',
startTime: '07:30:00.0000000',
endTime: '08:00:00.0000000'
}]
}
console.log(Object.keys(obj))
var obj= { 'Cloud Revenue':
[ { sessionName: 'Business Growth',
channelName: 'Cloud Revenue',
channelId: '19:[email protected]',
startDate: '2020-04-12',
endDate: '2020-04-12',
startTime: '08:00:00.0000000',
endTime: '08:30:00.0000000' },
{ sessionName: 'Increase Market Value',
channelName: 'Cloud Revenue',
startDate: '2020-04-12',
endDate: '2020-04-12',
startTime: '06:30:00.0000000',
endTime: '07:00:00.0000000' } ],
General:
[ { sessionName: 'Value Proposition',
channelName: 'General',
startDate: '2020-04-12',
endDate: '2020-04-12',
startTime: '07:30:00.0000000',
endTime: '08:00:00.0000000' } ] }
for(keys in obj){
console.log(keys);
}