0

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 inJAVASCRIPT

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"}];
4
  • Ummm nativearr.filter(o => !newarr.some(e => e['_id'] === o['_id'])) Commented Apr 27, 2018 at 17:58
  • This example did not help me as I want. Commented Apr 27, 2018 at 18:01
  • Why not? that returns an array of objects which don't exist in newarray. Commented Apr 27, 2018 at 18:08
  • it is returning me the whole new array Commented Apr 27, 2018 at 18:11

2 Answers 2

1

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

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

4 Comments

I understood what is taking by parameters, is the lack of _id but the _id may be that all have, in fact the expected behavior is that it disregards those who do not have _id
It will have _id in the newarray that does not have in the oldarray, however I need the _id that had in the oldarray and agor does not have in the newarray
@RenanRodrigues it's unclear what you're asking. This approach returns what you're waiting for. Please, post more info in the question's body.
Do not ask me why else when I put it this way it worked the way I need it let item = oldArray.filter(o => !newArray.some(e => { return e._id.toString() === o._id.toString() }))
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.

3 Comments

The diffObjects helps me in this sense when I have to see items that have in one and not in another, but now I need the proprip diff, as in the example cited above
@RenanRodrigues have you tried it? If it didn't work, switch the argument order.
Yes it works for me, when I am comparing the changed items (which in the sense of my question is what has in the new and does not have in the old) but in my case I have in the old and not in the new, logically it should work well, I do not see why it does not work.

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.