-1

I am trying to simply add an updated object to an object array the struggle comes because the array is nested in the below structure.

Parent Object
id;
subObject[];

Sub Object
id;
subSubObject[];

Sub Sub Object
name;

Parent Object > Sub Object > Sub Sub Object to be added to the array of the Sub Object.

Let me know if you need any more information.

2
  • What's the error? Commented Nov 27, 2018 at 9:19
  • What's your query? Whats your expectation? Brief more... Commented Nov 27, 2018 at 9:24

1 Answer 1

0

Simplistically, you can chain the push as shown below:

const example = {
    id: 1,
    subObject: [
        {
            id: 2,
            subSubObject: [
                {
                    name: 'Jackson'
                }
            ]
        }
    ]
};

// Simplistic Example:
example.subObject[0].subSubObject.push({ name: 'Fenton' });
console.log(JSON.stringify(example));

// Assuming you want to push it onto a particular sub object:
example.subObject.find((item) => item.id === 2).subSubObject.push({ name: 'Toby' });
console.log(JSON.stringify(example));
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.