1

hi I want to push array of an object to array of an object so the the new property will push to the array of object based on the same _id this is the original data :

const data =[
             { 
              foo:foo1,
              data:{
                   _id:"a1",
                   man:2
                  }
            },
            { 
              foo:foo1,
              data:{
                   _id:"a1",
                   man:2
                }
             }
           ]

this is the data that I want to put on my original data

const d = [{
             _id:"a1",
             women:4,
           }]

And the desired output is:

      const data =[
             { 
              foo:foo1,
              data:{
                   _id:"a1",
                   man:2,
                   women:4
                  }
            },
            { 
              foo:foo1,
              data:{
                   _id:"a1",
                   man:2,
                   women:4
                }
             }
           ]

I think it can be done using for loop and check if the _id is the same and push it to the object, but is there any better way? or using lodash? any idea? thanks in advance

1
  • If I am understanding correctly you could just always add it and then use lodash unique on the updated array filter out all duplicates: lodash.com/docs/4.17.15#uniqBy. Not sure if this is slower performance than looping. Commented Apr 24, 2022 at 14:32

2 Answers 2

1

You can try this!

const d = [{
             _id:"a1",
             women:4,
           }
           ]
    
const data =[
             { 
              foo:"foo1",
              data:{
                   _id:"a1",
                   man:2
                  }
            },
            { 
              foo:"foo1",
              data:{
                   _id:"a1",
                   man:2
                }
             }
           ]    
var arr = data.map(function(obj, i){
    d.map(function(o,i){
    if(obj.data._id == o._id)
    {
        obj.data.women = o.women;
    }
    });
    return obj;
});

console.log(arr);

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

3 Comments

hi matee, thanks for reply but im sorry there is misleading here , i want to push array of an object to array of an object and the const d should be an array
Hi bramasta, I have updated my answer please have a look, Thanks.
Hi Mateen , thank you for updating the code, I will accept this as an answer
1

With the data structures you have you can loop through both and simply update the entries with the same id. That said this is a brute force solution with O(n^2) time complexity. This solution also returns a new object rather than mutating the original.

// Existing entries
const entries = [
    {
        data: {
            id: 'a1',
            man: 2
        }
    },
    {
        data: {
            id: 'a1',
            man: 2
        }
    },
];

// Data with same id to update
const updates = [
    {
        id: 'a1',
        women: 4
    },
];

/**
 * Update properties for an existing entry given
 * a list of partially updated entries
 */
function updateEntries(entriesList, updatesList) {
    // Build up new array as to not mutate
    // the existing data structure
    const newEntries = [];
    for (const entry of entriesList) {
        for (const update of updatesList) {
            // Update when the ids match
            if (entry.data.id === update.id) {
                newEntries.push({
                    data: {
                        ...entry.data,
                        ...update
                    }
                });
            } else {
                newEntries.push(entry);
            }
        }
    }
    return newEntries;
}

const newEntries = updateEntries(entries, updates);
// newEntries = [
//     {
//         data: {
//             id: 'a1',
//             man: 2,
//             women: 4
//         }
//     },
//     {
//         data: {
//             id: 'a1',
//             man: 2,
//             women: 4
//         }
//     },
// ];

If you change your data structure for entries to be and object with each entry organized by id assuming they should actually be unique anyway, you can get O(n) based on updates.

// Entries by id
const entries = {
    a1: {
        man: 2
    },
    a2: {
        man: 2
    },
};

4 Comments

hi bradcush, thank you for reply , I already figure it out this way but i think this is not the best way to do it , is there a better way? maybe using lodash? thanks
If you have full control over the shape of your data for entries and updates the algorithm can be a bit simpler and it's possible you can leverage lodash to make this type of update more concise but it's definitely not needed.
Hi bradcush, thanks for the idea , i will look for that
I've also updated the answer to include what this alternative entries data structure could look like if your entries can be unique by id.

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.