0

I am trying to remove the first object from a nested array but somehow I am not able to delete the first object here is my code can you please help?

var arr = [
        {
          demo: [
            {
              label: "NOT - Notification",
              id: "NOT",
              subTree: null,
            },
            { 
              label: "LIM - Limitation", 
              id: "LIM", 
              subTree: null 
            },
          ],
    },
];
      
var ind = arr.findIndex(function (element) {
  return element.demo?.id === "NOT";
});
if (ind !== -1) {
  arr.splice(ind, 1);
}
console.log('this is new', arr);

If you have any better solution then feel free to drop will appreciate your help.

3
  • It is an array inside an object inside an array. Please check again Commented Oct 17, 2022 at 15:09
  • Yes, that's right. Commented Oct 17, 2022 at 15:10
  • You're calling splice on the top-most array, not the array property of the object within the array. Call splice on the correct array, and it will work. Commented Oct 17, 2022 at 15:13

3 Answers 3

1

You are accessing the wrong array dimension. Check each subarray in the array:

var arr = [
    {
        demo: [
            {
                label: "NOT - Notification",
                id: "NOT",
                subTree: null,
            },
            { 
                label: "LIM - Limitation", 
                id: "LIM", 
                subTree: null 
            },
        ],
    },
];

for (const item of arr) {
    const index = item.demo.findIndex(subitem => subitem.id === "NOT");
    if (index >= 0) item.demo.splice(index, 1)
}

console.log('this is new', arr);
Sign up to request clarification or add additional context in comments.

4 Comments

There is no "dimension" here... Or rather, there is only a single dimension.
Since it is a nested array, it has two dimensions
It does not remove a first object if you check in the console we get nothing
@MilanSachani Yeah I had a typo in the code. I fixed it
1

just add the below snippet:

const newArr = arr.map(data => {
    return { demo : data?.demo.filter(d => d.id != "NOT") }
})
console.log(newArr)

Explanation :
Here, I'm looping through each main array, entering into objects, and then filtering only those with id other than "NOT".

Comment if you stuck with anything in the above code.

Comments

1

As per my understanding after looking in your code, You want to filter out the object which contains id as NOT. If Yes, You can simply achieve that by using Array.filter() method.

Live Demo :

var arr = [
      {
        demo: [
          {
            label: "NOT - Notification",
            id: "NOT",
            subTree: null,
          },
          { 
            label: "LIM - Limitation", 
            id: "LIM", 
            subTree: null 
          },
        ],
  }];
  
const res = arr.map(({ demo }) => demo.filter(({ id }) => id !== 'NOT'));

console.log(res);

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.