简体   繁体   中英

better way push array of an object to array of an object based on the same id?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM