admin管理员组

文章数量:1389750

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

All i need to pare 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;
        }
    }
}

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

All i need to pare 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;
        }
    }
}
Share Improve this question edited Jan 27, 2016 at 11:10 TheKingPinMirza asked Jan 27, 2016 at 10:59 TheKingPinMirzaTheKingPinMirza 8,9826 gold badges53 silver badges83 bronze badges 2
  • 2 you can add the matched items to a new array, like that newarray only contains matched elements – Florian Humblot Commented Jan 27, 2016 at 11:04
  • 1 Do you want to remove non-matched objects? The code seems to remove matched objects. Another thing is, whether it shout not loop from top index to zero index in array1 and remove elements from the end of the array. – Martin Staufcik Commented Jan 27, 2016 at 11:04
Add a ment  | 

5 Answers 5

Reset to default 2

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 ment, 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; }));
});
var array1 = [1,2,3,4,5];
var array2 = [3,4];
var array3 = array1.filter(i => array2.indexOf(i) !== -1);

https://jsfiddle/pLrr3or5/1/

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

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.

本文标签: javascriptcompare two arrays and remove not matched objectsStack Overflow