skip to Main Content

I am calling an API and got a response an an object in the following form. I would like to append the objects to an array in order to iterate them later.

Using push I got a list of strings == ['Account','AccountName','AccountServiceHomepage']
I would like to push the entire object to the array so for x[0] I get Account as an object not as a string.

Does anyone have a clue?

Snippet

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);

4

Answers


  1. You can convert object to an array (ignoring keys) like this:

    const items = Object.values();
    
    Login or Signup to reply.
  2. let x = Object.values(properties)
    
    Login or Signup to reply.
  3. Try this:

    for (let property in properties) {
        propertiesArray.push(properties[property]);
    }
    
    Login or Signup to reply.
  4. 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);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search