5

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.

22
  • 1
    Can you share sample arrays? Commented Oct 17, 2016 at 9:23
  • ES6 filter? Do you mean ES5 filter? Commented Oct 17, 2016 at 9:23
  • @Rajesh Object has an "Email" property. That should be fine. Commented Oct 17, 2016 at 9:23
  • Are you trying to delete the key, remove the value/ reference or remove the value from memory? Commented Oct 17, 2016 at 9:24
  • @evolutionxbox ECMAScript is probably what I should call it. I still haven't figured the difference out. Commented Oct 17, 2016 at 9:25

6 Answers 6

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);
});
Sign up to request clarification or add additional context in comments.

Comments

3

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

9 Comments

Yeah that works. Thanks! Is that the best way, though? I'm assuming the fastest would be O(n^2) anyway?
@Snorlax fastest way would be to index unique email-ids from both arrays and then match each item one by one. In that case, complexity would O(2n)
Yeah. With < 10 items, this should be fast enough though.
@Snorlax with less than 10 items, there shouldn't be any problem.
I would suggest you use array.findIndex or array.find or array.some. We do not need to find all elements and should break upon first match
|
3

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

3 Comments

Doesn't look like my Typescript validation is happy about Set.. hmm.
es6 !== typescript.
I never said that it was.
-1

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.. :)

1 Comment

n.. no. That's not what I want.
-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")
);

Comments

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

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.