I have this 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'] }
]
}
]
And this object:
const newItem = { name: 'It is a new item', numbers: ['12345'] }
As you can see in the array, it contains an object which contains two arrays of objects. The key of each of these nested arrays is a number, and as you can see the key number is also contained in the numbers array of each item, for example:
{ name: 'item one', numbers: ['12345', '77484'] },
That contains the key '12345' in its numbers array, therefore '12345' is the parent of that object.
I would like to take the number of the newItem, match it to a parent key and add it to the array that corresponds to the matching key parent.
Please take into account that this is dummy data, and imagine that the numbers array in newItem is a dynamic value and the nested array has lots of values. The idea here is to match these two numbers to know where the newItem should be pushed.
array[0][newItem.numbers[0]].push(newItem);newItemhad 2 numbers what is your expected result?arrayarray has two elements, also, this is hypothetical. Imagine there were more than two, maybe hundreds. ThenewItemwould be an item provided by a user via form, for example. IfnewItemhas more than one or two numbers, what would be ideal is that we take the numbers innewItemand check if in thearrayarray if that number exists to know where it should be pushed.