【问题标题】:loop list of objects with numbers [duplicate]带有数字的对象的循环列表[重复]
【发布时间】:2020-10-04 00:41:59
【问题描述】:

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

    {
  '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
  },

我正在尝试使用

获取 ID
Object.map(a => a.id);

但由于数字的原因,我无法获取 ID

【问题讨论】:

  • 您发布的内容不是(在 JavaScript 术语中)列表;这是一个普通的对象。

标签: javascript


【解决方案1】:

如果您对数字属性名称不感兴趣,可以使用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>

【讨论】:

    猜你喜欢
    • 2021-05-11
    • 1970-01-01
    • 2015-05-06
    • 2019-06-30
    • 1970-01-01
    • 2021-07-16
    • 2017-01-23
    • 2018-09-12
    • 1970-01-01
    相关资源
    最近更新 更多