3

I'm trying to add objects to an existing array if the condition is true.

Below is my code

RequestObj = [{
    "parent1": {
        "ob1": value,
        "ob2": {
            "key1": value,
            "key2": value
        },
    },
},
{
    "parent2": {
        "ob1": value,
        "ob2":{
            "key1":value,
            "key2": value
        }
    }
}]

Here I'm trying to add an object to the RequestObj array if the condition is true. I could do RequestObj.push(). But i don't know how to added in parent1 object.

if (key3 !== null) {
    // add this below object to parent1 object
    "ob3": {
        "key1": value,
        "key2": value
    }
}

I'm not able to find any solution. Please help

6
  • its an array of objects.. i missed the brackets Commented Mar 27, 2018 at 19:12
  • 1
    Your brackets are still mismatched Commented Mar 27, 2018 at 19:13
  • If I understood you correctly, you want to overwrite the key1 and key2 in a parent1 or parent2 object. Is that correct? Commented Mar 27, 2018 at 19:18
  • What is the expected final object you want? Commented Mar 27, 2018 at 19:19
  • "parent1": { "ob1": value, "ob2": { "key1": value, "key2": value },"ob3": { "key1": value, "key2": value } Commented Mar 27, 2018 at 19:24

2 Answers 2

3

The way to add an element to an array is to push it.

// Create new object
var newObject = {
    "ob3": {
        "key1": value,
        "key2": value
    }
};
// Add new object to array
RequestObj.push(newObject);

You can also directly push an object into the array without declaring a variable first:

// Add new object to array
RequestObj.push({
    "ob3": {
        "key1": value,
        "key2": value
    }
});

UPDATE

If you are not pushing into the array but adding a new property to an object inside the array, you need to know the position of the element inside the array, like RequestObj[0] for the first element.

Then within that element, you need to add a new property to the parent1 object (RequestObj[0].parent1):

RequestObj[0].parent1.ob3 = {
      "key1": "A",
      "key2": "B"
 };

var RequestObj = [{
  "parent1": {
    "ob1": "A",
    "ob2": {
      "key1": "B",
      "key2": "C"
    },
  },
  "parent2": {
    "ob1": "D",
    "ob2": {
      "key1": "E",
      "key2": "F"
    }
  }
}];

var key3 = 'somethingNotNull';

if (key3 !== null) {
  RequestObj[0].parent1.ob3 = {
      "key1": "A",
      "key2": "B"
  };
}
console.log(RequestObj);

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

3 Comments

i need to push the ob3 object inside parent1.
every parent 1? or just the first item in the array
only to parent1, its may not be the first item
0

You shoud iterate RequestObj in loop and find object by key parent1.

For that you can use .filter function like this:

for(let i = 0; i < RequestObj.lenght; i++) {
   let found_object = RequestObj[i].filter(function (r) {
      return r == 'parent1'
   });
}

Then you can execute operations over found_object.

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.