0

I have object with array like that:

const result = {
    key1: [],
    key2: [],
    key3: [],
    key4: []
};

And I want to push to one of the "key" something like that:

result.key1.push({key11: []})
result.key1.push({key12: []})
result.key1.push({key13: []})

But I need the result looks like that:

{
    key1: [
        key11: [],
        key12: [],
        key13: []
    ],
    key2: [],
    key3: [],
    key4: []
}

I tried almost everything did I miss something?

2
  • 2
    Arrays have keys that are positive integers, key11 is a object property Commented Jan 25, 2020 at 15:47
  • Rephrasing, Arrays can only have keys that are positive integers Commented Jan 25, 2020 at 15:48

1 Answer 1

2

You're mixing up objects and arrays. Arrays have items in order, from 0 to length - 1, while objects have named keys. It seems that you're looking for having named keys, so you need to create an object instead.

const result = {
    key1: {},
    key2: {},
    key3: {},
    key4: {},
};

Now simply assign items.

result.key1.key11 = []
result.key1.key12 = []
result.key1.key13 = []
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.