0

I have an array like so:

const array = [
    {
        '12345': [
            {
                name: 'item one',
                numbers: ['12345', '77484'],
            },
            {
                name: 'item two',
                numbers: ['12345', '65456'],
            },
            {
                name: 'item three',
                numbers: ['12345', '33920'],
            },
            {
                name: 'item four',
                numbers: ['12345', '99393'],
            },
        ],
    },
    {
        '67890': [
            {
                name: 'item one b',
                numbers: ['67890', '33232'],
            },
            {
                name: 'item two b',
                numbers: ['67890', '33456'],
            },
            {
                name: 'item three b',
                numbers: ['67890', '77665'],
            },
            {
                name: 'item four b',
                numbers: ['67890', '11234'],
            },
        ],
    },
]
console.log(array);

If I am given a name as a dynamic variable, for example, 'item three b', how can find that name inside the array of objects of the array of objects to delete it?

I'm stumped when trying to get into the nested array.

Thanks!

6
  • Welcome to Stack Overflow! Please visit help center, take tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output, preferably in a Stacksnippet Commented Jul 28, 2020 at 17:29
  • Please clarify what you are trying to do. Commented Jul 28, 2020 at 17:33
  • With either a nested loop or map of find, you could get the object. What are you then wanting to do with it? Delete it? That makes it a bit different. Commented Jul 28, 2020 at 17:33
  • Hi, yes. if I am given a name, such as item three b, how do I find it in the nested array and delete it. Thanks. I've tried find() but I cannot get into the nested array with find(). I can loop through the first level of objects, but I am stumped when getting into the nested array. Commented Jul 28, 2020 at 17:39
  • Do you want to delete the whole property that contains the name, or just the individual array element. Commented Jul 28, 2020 at 17:59

1 Answer 1

1

You can achieve this using forEach loop , then using findIndex and finally splice the data from the array.

const array = [
    {
        '12345': [
            {
                name: 'item one',
                numbers: ['12345', '77484'],
            },
            {
                name: 'item two',
                numbers: ['12345', '65456'],
            },
            {
                name: 'item three',
                numbers: ['12345', '33920'],
            },
            {
                name: 'item four',
                numbers: ['12345', '99393'],
            },
        ],
    },
    {
        '67890': [
            {
                name: 'item one b',
                numbers: ['67890', '33232'],
            },
            {
                name: 'item two b',
                numbers: ['67890', '33456'],
            },
            {
                name: 'item three b',
                numbers: ['67890', '77665'],
            },
            {
                name: 'item four b',
                numbers: ['67890', '11234'],
            },
        ],
    },
]

array.forEach(obj => {
   Object.values(obj).forEach(ob => {
   var index = ob.findIndex(o => o.name==='item three b');
   if(index>-1){
      ob.splice(index,1);
     }
  })
})

console.log(array);

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

1 Comment

This worked like an absolute charm! Thank you so much!

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.