Sememoir
Sememoir

Reputation: 65

Find Object key name and add object to matching array

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.

Upvotes: 1

Views: 81

Answers (4)

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

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);

Upvotes: 1

Daniel Mesquita
Daniel Mesquita

Reputation: 136

Try that:

array.map((row) => {
  Object.keys(row).map((value) => {
    if (newItem.numbers.indexOf(value) >= 0) {
      row[value].push(newItem);
    }
  });
});

Upvotes: 2

Rahul Bhobe
Rahul Bhobe

Reputation: 4451

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); 

Upvotes: 1

GirkovArpa
GirkovArpa

Reputation: 4912

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);

Upvotes: 0

Related Questions