遍历对象,其中Value是对象数组,并从对象数组的每个对象返回一个值

时间:2019-12-12 16:11:29

标签: javascript arrays object

如何从中获取所有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))))

3 个答案:

答案 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)

将所有内容转换为数组并将其展平有很大帮助!

https://codepen.io/t0dorakis/pen/MWYerKL