0

I am trying to convert an object (updatedConfig) to an array (configArray), while maintaining the same structure (and avoid further nesting).

I have tried declaring a new array const and using Object.entries to push the keys & values. I am able to get the keys but am having trouble figuring out how to achieve the nesting of array.

const configArray = [];
    
    Object.entries(updatedConfig).forEach(([key, value]) => {
        configArray.push(key);     
    })

Here is the object in question: enter image description here

Jordan
  • 103
  • 6
  • 1
    Could you give an example of the output you're looking for? – lucasvw Jun 30 '22 at 14:28
  • 1
    [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) For code like that shown, use `console.log(JSON.stringify(obj, null, 2))` – Heretic Monkey Jun 30 '22 at 14:28
  • For the question, there is an `Object.keys(obj)` method that gets just the keys, no need to get the entries and pull just the key... – Heretic Monkey Jun 30 '22 at 14:29
  • Does this answer your question? [Get array of object's keys](https://stackoverflow.com/questions/8763125/get-array-of-objects-keys), specifically, [this answer](https://stackoverflow.com/a/55310101/215552). – Heretic Monkey Jun 30 '22 at 14:31
  • I need the array's associated with the keys too – Jordan Jun 30 '22 at 14:34
  • 1
    @Jordan Could you update the question to include what the output should look like? I don't understand what structure you're hoping to create. – lucasvw Jun 30 '22 at 14:35

1 Answers1

1

You can try something like this using Object.entries and Array.map

const configObject = {
 key1: 'value',
 key2: 1,
 key3: [1, 2, 3]

}

const configArray = Object.entries(configObject).map(([key, value]) => ({key, value}))

console.log(configArray)
R4ncid
  • 6,944
  • 1
  • 4
  • 18