4

I have this:

var arrA = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];

I have another array:

var arrB = [{id:1,other:'c'},{id:3,other:'d'}];

How can I remove the items from arrA that have property id same as arrB using underscore.js?

The expected result should be:

arrA = [{id:2, name:'b'}];

Thanks,

5
  • Must be used underscoreJS? Commented Aug 10, 2016 at 11:33
  • javascript would be too messy. or you have any other libraries? lodash would be fine too. Commented Aug 10, 2016 at 11:34
  • I have only javascript solution Commented Aug 10, 2016 at 11:34
  • ok, lets post a plunkr or jsfiddle in the comment here. don't post the answer, might get downvote because that's not answering the question. thanks for the contribution. Commented Aug 10, 2016 at 11:36
  • Here is the link. Check the browser console to see the result jsfiddle.net/y9tsee7g Commented Aug 10, 2016 at 11:41

5 Answers 5

6

Using Array#filter and Array#findIndex

var output = arrA.filter((el) => {
  return arrB.findIndex((elem) => {
    return elem.id == el.id;
  }) == -1;
});

One liner:

arrA.filter((el) => (arrB.findIndex((elem) => (elem.id == el.id)) == -1));

var arrA = [{
  id: 1,
  name: 'a'
}, {
  id: 2,
  name: 'b'
}, {
  id: 3,
  name: 'c'
}];


var arrB = [{
  id: 1,
  other: 'c'
}, {
  id: 3,
  other: 'd'
}];

var op = arrA.filter(function(el) {
  return arrB.findIndex(function(elem) {
    return elem.id == el.id;
  }) == -1;
});
console.log(op);

Or using Array#find

var arrA = [{
  id: 1,
  name: 'a'
}, {
  id: 2,
  name: 'b'
}, {
  id: 3,
  name: 'c'
}];


var arrB = [{
  id: 1,
  other: 'c'
}, {
  id: 3,
  other: 'd'
}];

var op = arrA.filter(function(el) {
  return !arrB.find(function(elem) {
    return elem.id == el.id;
  });
});
console.log(op);

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

Comments

1

Like this

var arrA = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];
var arrB = [{id:1,other:'c'},{id:3,other:'d'}];
var keys = _.keys(_.indexBy(arrB, "id"));
var result = _.filter(arrA, function(v) {
   return !_.contains(keys, v.id.toString());
});
console.log(result)

Hope this helps.

Comments

1

In pure javascript you can use forEach() loop and splice() to remove object if it's id is found in other array.

var arrA = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];
var arrB = [{id:1,other:'c'},{id:3,other:'d'}];

var b = arrB.map(e => e.id);

arrA.forEach(function(e, i) {
  if(b.indexOf(e.id) != -1) arrA.splice(i, 1);
});

console.log(arrA);

Comments

1

var arrA = [{id:1,name:'a'}, {id:2,name:'b'}, {id:3,name:'c'}];
var arrB = [{id:1,other:'c'}, {id:3,other:'d'}];

var res = arrB.reduce((acc, b) => {
  return acc.filter(({id}) => id !== b.id);
}, arrA);

// [{id:2,name:'b'}]
console.log(res);

Comments

0

Sounds like you want the difference, but unfortunately that won't work for objects. Instead you could try this:

arrA = _.filter(arrA, function(obj){
           return !_.findWhere(arrB, {id: obj.id});
       });

You could do this without underscore using the build in filter and find functions.

Comments

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.