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

6
  • 1
    Could you give an example of the output you're looking for? Commented Jun 30, 2022 at 14:28
  • 1
    Please do not upload images of code/data/errors when asking a question. For code like that shown, use console.log(JSON.stringify(obj, null, 2)) Commented Jun 30, 2022 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... Commented Jun 30, 2022 at 14:29
  • Does this answer your question? Get array of object's keys, specifically, this answer. Commented Jun 30, 2022 at 14:31
  • 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. Commented Jun 30, 2022 at 14:35

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.