1

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.

5
  • Simply like this array[0][newItem.numbers[0]].push(newItem); Commented Jul 29, 2020 at 5:26
  • Sure, but that wouldn't work if I had to search through many more items in the array and that the given newItem had a dynamic value for me to match to the elements in that array. I'll clarify my question. Thanks! Commented Jul 29, 2020 at 7:04
  • @ale_aalt - why do you have an array of objects at top level with just one element? Shouldn't you have only an object? Commented Jul 29, 2020 at 8:01
  • Could you also confirm what is expected if your newItem had 2 numbers what is your expected result? Commented Jul 29, 2020 at 8:04
  • Hi Rahul, thanks for your comment. Don't know exactly what you mean about an array of objects at top level with just one element. As you can see the array array has two elements, also, this is hypothetical. Imagine there were more than two, maybe hundreds. The newItem would be an item provided by a user via form, for example. If newItem has more than one or two numbers, what would be ideal is that we take the numbers in newItem and check if in the array array if that number exists to know where it should be pushed. Commented Jul 29, 2020 at 11:56

4 Answers 4

2

Try that:

array.map((row) => {
  Object.keys(row).map((value) => {
    if (newItem.numbers.indexOf(value) >= 0) {
      row[value].push(newItem);
    }
  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that works!! One question: what would be the benefit in using map() vs desctructuring as sivako suggests above? Thank you again!
I think the answer above means .forEach instead of .map, although in this case they are practically the same.
For a case like this, relatively simple, they are indeed practically the same. The difference is that forEach doesn't return anything, you need to mutate the original for your case. With .map(), you get a new array returned back, so technically you can assign the answer above to a new constant and you still have the original array intact. I personally like the .map() better.
1

You could do this:

    for (let i=0; i<array.length; i++) {
        array[i][newItem.numbers[0]] = array[i][newItem.numbers[0]] || [];
        array[i][newItem.numbers[0]].push(newItem);
    }

First line initializes the nested array if it does not exist. And the second line pushes the new item to the nested array.

See code snippet below.

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'] },
        ],
    }
]


const newItem = {
    name: 'It is a new item',
    numbers: ['12345'],
}


for (let i=0; i<array.length; i++) {
    array[i][newItem.numbers[0]] = array[i][newItem.numbers[0]] || [];
    array[i][newItem.numbers[0]].push(newItem);
}

console.log(array); 

Comments

1

With help of destructuring.

const [obj] = array;
const {
  numbers: [key],
} = newItem;
obj[key].push(newItem);

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"],
      },
    ],
  },
];

const newItem = {
  name: "It is a new item",
  numbers: ["12345"],
};

const [obj] = array;
const {
  numbers: [key],
} = newItem;
obj[key].push(newItem);

console.log(array);

1 Comment

Thank you, that works!! One question: what would be the benefit in using desctructuring vs. map() as Daniel Mesquita suggests below? Thank you again!
0

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'] }
    ]
  }
]

const newItem = { name: 'It is a new item', numbers: ['12345'] }

newItem.numbers.forEach(n => array[0]?.[n].push(newItem));

console.log(array);

2 Comments

Looking at the data only numbers[0] seems to be the parent. Although that would have to be clarified by OP.
Sure, but that wouldn't work if I had to search through many more items in the array and that the given newItem had a dynamic value for me to match to the elements in that array. I'll clarify my question. Thanks!

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.