最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove object from array if it is contained in another array - Stack Overflow

programmeradmin0浏览0评论

I am trying to remove an object from an array, if that object's property (unique) is included in the other array. I know I can do a nested for-loop like this:

for(i = 0; i < array.length; i++) {
    for(j = 0; j < array2.length; j++) {
        if(array[i].Email === array2[j].Email) {
            //remove array[i] object from the array
        }
    }
}

Or whatever. Something like that. Is there an ES6 filter for that? I can easily do a filter up against a regular array with strings, but doing it with an array of objects is a bit more tricky.

I am trying to remove an object from an array, if that object's property (unique) is included in the other array. I know I can do a nested for-loop like this:

for(i = 0; i < array.length; i++) {
    for(j = 0; j < array2.length; j++) {
        if(array[i].Email === array2[j].Email) {
            //remove array[i] object from the array
        }
    }
}

Or whatever. Something like that. Is there an ES6 filter for that? I can easily do a filter up against a regular array with strings, but doing it with an array of objects is a bit more tricky.

Share Improve this question asked Oct 17, 2016 at 9:22 MortenMoulderMortenMoulder 6,64614 gold badges72 silver badges122 bronze badges 22
  • 1 Can you share sample arrays? – Rajesh Commented Oct 17, 2016 at 9:23
  • ES6 filter? Do you mean ES5 filter? – evolutionxbox Commented Oct 17, 2016 at 9:23
  • @Rajesh Object has an "Email" property. That should be fine. – MortenMoulder Commented Oct 17, 2016 at 9:23
  • Are you trying to delete the key, remove the value/ reference or remove the value from memory? – maximumcallstack Commented Oct 17, 2016 at 9:24
  • @evolutionxbox ECMAScript is probably what I should call it. I still haven't figured the difference out. – MortenMoulder Commented Oct 17, 2016 at 9:25
 |  Show 17 more comments

6 Answers 6

Reset to default 11

If you are fine using ES6, you can even look into array.find, array.filter or array.some.

Array.findIndex

const result = array.filter(x => {
    return array2.findIndex(t => t.Email === x.Email) === -1;
});

Array.some

const result = array.filter(x => {
    return !array2.some(t => t.Email === x.Email);
});

Not very optimal, but try this

array = array.filter( function( item ){
  return array2.filter( function( item2 ){
    return item.Email == item2.Email;
  }).length == 0;
});

Try with find as well, it won't iterate all the elements and will break after first match itself

array = array.filter( function( item ){
  return array2.find( function( item2 ){
    return item.Email == item2.Email;
  }) == undefined;
});

You could use a Set with ES6

var array = [/* your data */],
    array2 = [/* your data */],
    set = new Set(...array2.map(a => a.Email));

array = array.filter(a => !set.has(a.Email));

Try this one.... :)

if(array[i].Email === array2[j].Email){
   // splice(Index you want to remove,to witch element)
   array1.splice(i,1);
}

splice() can remove element of your array. You need to pass witch element. witch element is the start of delete. How many elements should delete. That's 1.. :)

How about writing a function, passing the parameters and simply collecting the output?

function arrNotInArrKey(arr1, arr2, key) {
  for (var i = 0; i < arr1.length; i++) {
    for (var j = 0; j < arr2.length; j++) {
      if (arr1[i][key] === arr2[j][key]) {
        arr1.splice(i, 1);
        i--;
      }
    }
  }
  return arr1;
}
console.log(
  arrNotInArrKey([{
    name: 1
  }, {
    name: 3
  }, {
    name: 2
  }], [{
    name: 2
  }], "name")
);

You can use shift function. here it's example.

http://www.w3schools.com/js/tryit.asp?filename=tryjs_array_shift
for(i = 0; i < array.length; i++) {
    for(j = 0; j < array2.length; j++) {
        if(array[i].Email === array2[j].Email) {
            array.shift(array[i].Email);
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论