下划线组通过迭代结果

问题描述 投票:0回答:2

我有一个对象数组,我使用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,没有任何东西可以为我提供顶级属性字符串。我该如何实现?

javascript arrays underscore.js
2个回答
0
投票

您可以将香草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))

0
投票
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);
}
© www.soinside.com 2019 - 2024. All rights reserved.