0

I have 2 arrays of objects.
Array1:

[{ id: 2, ref: 1010101 },
 { id: 2, ref: 1010107 }]

Array2:

[{ id: 2, ref: 10010001 },
 { id: 2, ref: 10010002 },
 { id: 2, ref: 10010003 },
 { id: 2, ref: 10010006 },
 { id: 2, ref: 10010007 },
 { id: 2, ref: 10010008 },
 { id: 2, ref: 10010009 },
 { id: 2, ref: 10020005 },
 { id: 2, ref: 1010101 }]

I need to do 2 tasks:
1. Find what elements of array1 are in array2 . After find Items, Im going to do something with that elements.
2. Remove elements found from array2. After I do something with my found items, I need to remove each item found.

I was trying to do it with lodash but without success.

I need something like this:

if(elementFound) {
  //do something
  doSomething(item);
  //after it, remove from array2
  removeItemFromArray2(item);
}

Thanks, I think there is an easy way to do it with lodash.

4
  • 2
    Why use lodash when you've got indexOf and splice? Commented Mar 2, 2016 at 20:05
  • 1
    Array#filter() should do. Commented Mar 2, 2016 at 20:06
  • Possible duplicate of Compare two Javascript Arrays and remove Duplicates Commented Mar 2, 2016 at 20:07
  • 1
    Since two objects with the same key/values aren't the same, you can only use indexOf if you stringify each object first. Depending on the size of the array, comparing strings and then slicing out the index from the original array might outperform using a nested filter which will do multiple loops. Commented Mar 2, 2016 at 20:17

4 Answers 4

2

Try to use Array.prototype.filter at this context to achieve what you want,

var filteredArray = Array2.filter(function(itm){
 return Array1.filter(function(itmIn){
   return itmIn.id === itm.id && itmIn.ref === itm.ref;
 }).length == 0;
});
Sign up to request clarification or add additional context in comments.

1 Comment

what is length == 0 for? I dont get it
2

There is one specific lodash function for this:

var array1 = [
  { id: 2, ref: 1010101 },
  { id: 2, ref: 1010107 }
];

var array2 = [
  { id: 2, ref: 10010001 },
  { id: 2, ref: 10010002 },
  { id: 2, ref: 10010003 },
  { id: 2, ref: 10010006 },
  { id: 2, ref: 10010007 },
  { id: 2, ref: 10010008 },
  { id: 2, ref: 10010009 },
  { id: 2, ref: 10020005 },
  { id: 2, ref: 1010101 }
];

var res = _.differenceBy(array2, array1, 'ref');

document.write("<pre>" + JSON.stringify(res, null, 3) + "</pre>")
<script src="https://cdn.jsdelivr.net/lodash/4.6.1/lodash.min.js"></script>

1 Comment

I knew this lodash function, but I was looking for a find first (do something) and after it, remove it. But thanks anyway
1

There certainly is! You can use _.remove to programmatically remove elements from array2 if they meet a certain predicate. In your case, that predicate would be "is the element in array1",

var array1 = [{ id: 2, ref: 1010101 },
 { id: 2, ref: 1010107 }]

var array2 = [{ id: 2, ref: 10010001 },
 { id: 2, ref: 10010002 },
 { id: 2, ref: 10010003 },
 { id: 2, ref: 10010006 },
 { id: 2, ref: 10010007 },
 { id: 2, ref: 10010008 },
 { id: 2, ref: 10010009 },
 { id: 2, ref: 10020005 },
 { id: 2, ref: 1010101 }]

_.remove(array2, function (item) {
  return _.find(array1, item);
});

document.write("<pre>" + JSON.stringify(array2, null, 2) + "</pre>")
<script src="https://cdn.jsdelivr.net/lodash/4.6.1/lodash.min.js"></script>

Comments

1

A solution with a linear O.

var array1 = [{ id: 2, ref: 1010101 }, { id: 2, ref: 1010107 }],
    array2 = [{ id: 2, ref: 10010001 }, { id: 2, ref: 10010002 }, { id: 2, ref: 10010003 }, { id: 2, ref: 10010006 }, { id: 2, ref: 10010007 }, { id: 2, ref: 10010008 }, { id: 2, ref: 10010009 }, { id: 2, ref: 10020005 }, { id: 2, ref: 1010101 }],
    result = function (a, b) {
        function k(x) { return x.id + '|' + x.ref; }
        var o = {};
        b.forEach(function (c) {
            o[k(c)] = true;
        });
        a.forEach(function (c) {
            o[k(c)] = false;
        });
        return b.filter(function (c) {
            return o[k(c)];
        });
    }(array1, array2);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

1 Comment

This solutions it's very unclear for me, but thanks anyway.

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.