javascript 将多维对象推送到数组并获取对象数组[duplicate]

vlju58qv  于 2023-02-02  发布在  Java
关注(0)|答案(4)|浏览(157)
    • 此问题在此处已有答案**:

Get values from an object in JavaScript [duplicate](6个答案)
2小时前关门了。
我正在调用一个API,并得到了一个对象的响应,如下所示。我想将这些对象附加到一个数组中,以便以后对其进行迭代。
使用push,我得到了一个字符串列表== ['Account','AccountName','AccountServiceHomepage'],我想把整个对象推到数组中,所以对于x [0],我得到的是对象而不是字符串形式的Account。
有人知道吗?

    • 片段**
let properties = {
  Account: {
    label: 'Account',
    key: 'Account',
    description: { en: '' },
    prefLabel: { en: 'test' },
    usageCount: '0'
  },
  AccountName: {
    label: 'AccountName',
    key: 'AccountName',
    description: { en: '' },
    prefLabel: { en: '' },
    usageCount: '0'
  },
  AccountServiceHomepage: {
    label: 'AccountServiceHomepage',
    key: 'AccountServiceHomepage',
    description: { en: '' },
    prefLabel: { en: '' },
    usageCount: '0'
  }
}  
  
x = [];
for (i in properties) {
  x.push(i);
};

console.log(x);
xtfmy6hx

xtfmy6hx1#

let x = Object.values(properties)
nc1teljy

nc1teljy2#

let properties = {
  Account: {
    label: 'Account',
    key: 'Account',
    description: { en: '' },
    prefLabel: { en: 'test' },
    usageCount: '0'
  },
  AccountName: {
    label: 'AccountName',
    key: 'AccountName',
    description: { en: '' },
    prefLabel: { en: '' },
    usageCount: '0'
  },
  AccountServiceHomepage: {
    label: 'AccountServiceHomepage',
    key: 'AccountServiceHomepage',
    description: { en: '' },
    prefLabel: { en: '' },
    usageCount: '0'
  }
}

x = [];
for (i in properties) {
  x.push(properties[i]);
};

console.log(x);
vd8tlhqk

vd8tlhqk3#

你可以像这样把object转换成数组(忽略键):

const items = Object.values();
rqqzpn5f

rqqzpn5f4#

试试这个:

for (let property in properties) {
    propertiesArray.push(properties[property]);
}

相关问题