首页 > 解决方案 > 带有数字的对象的循环列表

问题描述

我正在遍历对象列表,但是在调用异步函数时,我得到了带有数字的对象数组,现在我无法映射数组。

    {
  '0': {
    id: '22be325d3564d713',
    name: 'dasdasdas',
    affiliateIds: 2,
    platformIds: 2,
    campaignIds: [],
    deviceOses: [],
    deviceTypes: [],
    countryCodes: [],
    entryType: 'RED',
    model: 'REVENUE_RATIO',
    value: 0.01,
    active: 1,
    createdAt: 2020-06-11T06:00:53.000Z,
    updatedAt: 2020-06-11T06:00:53.000Z
  },
  '1': {
    id: '2891e303645099fd',
    name: '',
    affiliateIds: [],
    platformIds: [],
    campaignIds: [],
    deviceOses: [],
    deviceTypes: [],
    countryCodes: [],
    entryType: 'CNV',
    model: 'REVENUE_RATIO',
    value: 0.7,
    active: 1,
    createdAt: 2018-01-09T11:12:29.000Z,
    updatedAt: 2018-01-09T11:12:29.000Z
  },

我正在尝试使用

Object.map(a => a.id);

但由于数字我无法获得身份证

标签: javascript

解决方案


Object.values()如果您对数字属性名称不感兴趣,可以使用

const res = Object.values(data).map(({id}) => id)

console.log(res)
<script>
const data={0:{id:"22be325d3564d713",name:"dasdasdas",affiliateIds:2,platformIds:2,campaignIds:[],deviceOses:[],deviceTypes:[],countryCodes:[],entryType:"RED",model:"REVENUE_RATIO",value:.01,active:1,createdAt:"2020 - 06 - 11 T06: 00: 53.000 Z",updatedAt:"2020 - 06 - 11 T06: 00: 53.000 Z"},1:{id:"2891e303645099fd",name:"",affiliateIds:[],platformIds:[],campaignIds:[],deviceOses:[],deviceTypes:[],countryCodes:[],entryType:"CNV",model:"REVENUE_RATIO",value:.7,active:1,createdAt:"2018 - 01 - 09 T11: 12: 29.000 Z",updatedAt:"2018 - 01 - 09 T11: 12: 29.000 Z"}};
</script>


推荐阅读