简体   繁体   中英

Difference between arrays of objects returning a new array

Well I need to find what does not have in one array for others, I think in an example I can explain better:

I have an array of objects as follows:

NATIVE: [{"_id": "asd12312312", "name": "item 01"}, {"_id": "1123rwerwe", "name": "item 02"}];

But I made an update with one value and added another, so it looked like this:

NEW: [{"_id": "1123rwerwe", "name": "item 02"}, {"name": "item 03"}];

This function needs to return me what was taken in another array, that is, I need this object of the result:

RESULT: [{"_id": "asd12312312", "name": "item 01"}];

Because comparing the "native array" and "new array" it would return what it has in the native and not in the new one, the purpose of this is to know if it removed some item.

Note that the new array exists an item without _id this is given because it added a new item, because who will generate _id is the database itself.

I'm using NODE JS so this function should be basically done in JAVASCRIPT

I have this function that tells me what it has in one and not in the other, it works very well, but for my return case only of what it does not have, it does not work:

const diffObjects = (object, base) => {
function changes(object, base) {
    return _.transform(object, function (result, value, key) {
        if (!_.isEqual(value, base[key])) {
            result[key] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
        }
    });
}
  return changes(object, base);
};

Something very important is the performance, because I have a N number of arrays to be sent to this function.

In my example you can see item without _id, in this case the item should be discarded from the logic, need to take into account only the items that have _id

Drawing I have two containers

01 [|||||||||||||]
02 [ |||||||||| |]

In this example I do not need the colored items, I need the items I had in 01 and now do not have in 02, you can see that the last point is colored, that is, it had no 01 and now it has 02, the embroidery is unlike this, what was in the 01 and was deleted in 03.

then the result would be

R [|          || ]

That is, it had on 01 and now it does not have on 02

If I use the logic you are talking about it is returning me like this:

RESULT: [{"name": "item 03"}];

You can use the function filter and function some to get the objects which don't exist within the new array.

This approach uses the attribute _id as the key/unique name between both arrays

 const native = [{"_id": "asd12312312", "name": "item 01"}, {"_id": "1123rwerwe", "name": "item 02"}], newArray = [{"_id": "1123rwerwe", "name": "item 02"}, {"name": "item 03"}], result = native.filter(o => !newArray.some(e => e['_id'] === o['_id'])); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

If I understand that code snippet correctly what diffObjects tells you is which elements are in base argument that are NOT in the object argument.

So call diffObjects with the NEW array as the base and the NATIVE array as the object .

I believe you didn't see that solution yourself because you were thinking in terms of the original array, and a new array. If you change how you look at it, to, "I just have 2 arrays and want to know which elements are in one and not the other" it becomes more obvious what the solution is.

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