TheKingPinMirza
TheKingPinMirza

Reputation: 8912

compare two arrays and remove not matched objects

I am struggling to compare two arrays of objects and removing not matched objects from first array.

All i need to compare two arrays (array1 and array2) of objects and remove NOT MATCHED objects from the array 1.

This is what i have done till now but it remove all items.

for (var i = 0, len = array1.length; i < len; i++) {
    for (var j = 0, len2 = array2.length; j < len2; j++) {
        if (array1[i].Id != array2[j].Student.Id) {
            array1.splice(j, 1);
            len= array1;
        }
    }
}

Upvotes: 0

Views: 9267

Answers (5)

Aaron Tomlinson
Aaron Tomlinson

Reputation: 285

2021

  const a1 = {a:1,b:2,c:3}
  const a2 = {a:1,b:2,c:4,d:6}
  const r = {}
  
  for(const k2 in a2){
    for(const k1 in a1){
      if(k1 === k2){
        r[k1] = a2[k1]
      }
    }
  } 

  console.log(r) // {a: 1, b: 2, c: 4}

These for loops run fast and clean in my opinion. Seems like the cleanest way to handle an object array.

Upvotes: 0

Rambabu Bommisetti
Rambabu Bommisetti

Reputation: 137

var Employees= [{name:"Ram",htno:1245},{name:"mohan",htno:1246},
{name:"madhu",htno:1247},{name:"ranga",htno:1248}]

var seletedEmployees= [{name:"mohan"},{name:"ranga"}];

var nonmatched = Employees.filter(function(val,index) { 
  return seletedEmployees.map(function(e) { return e.name; }).indexOf(val.name) == -1;
});

console.log(nonmatched);

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074138

If you're looping over array1 with i = 0, len = array1.length; i < len; i++, but within the loop you remove an entry from array1 what do you think happens on the next loop?

You also appear to be removing things that are found, but your question sayd you want to remove ones that aren't. In the below, in light of your comment, I'm removing things that aren't found.

In that case, use a while loop. I'd also use Array#some (ES5+) or Array#find (ES2015+) rather than doing an inner loop:

var i = 0;
var entry1;
while (i < array1.length) {
    entry1 = array1[i];
    if (array2.some(function(entry2) { return entry1.Id === entry2.Student.Id; })) {
        // Found, progress to next
        ++i;
    } else {
        // Not found, remove
        array1.splice(i, 1);
    }
}

Or if it's okay to create a new array, use filter:

array1 = array1.filter(function(entry1) {
    return array2.some(function(entry2) { return entry1.Id === entry2.Student.Id; }));
});

Upvotes: 2

Alisson Alvarenga
Alisson Alvarenga

Reputation: 658

You can do something like this:

var array1 = [1,2,3,4];
var array2 = [2,4,6,8];
var array3 = [];

for (var i = 0; i < array1.length; i++) {
    for (var j = 0; j < array2.length; j++) {
        if (array1[i] === array2[j]) {
            array3.push(array1[i]);           
        }
    }
}

console.log(array3);
> [ 2, 4 ]

Upvotes: 0

var array1 = [1,2,3,4,5];
var array2 = [3,4];
var array3 = array1.filter(i => array2.indexOf(i) !== -1);

https://jsfiddle.net/pLrr3or5/1/

Upvotes: 2

Related Questions