I currently have an array containing objects structured like this:
let arr = [
{ taxonomy: 'category', id: [ 10, 100 ] },
{ taxonomy: 'post_tag', id: [ 20 ] },
];
My goal is to insert a new object into the array with the following structure:
const object = {
taxonomy: 'category',
id: 30
}
I am looking for a solution to check if an object with the property value of 'taxonomy' already exists in the array. If it does, I want to add the id from the new object only to that existing object. Although I know how to check if the property already exists, I am unsure about how to properly add the new id to the array.
Upon adding the above-mentioned object, the resulting array should look like this:
[
{ taxonomy: 'category', id: [ 10, 100, 30 ] }, // Added 30
{ taxonomy: 'post_tag', id: [ 20 ] },
];
If the object does not yet exist, it should be added accordingly. Could someone provide assistance with this issue?