compare two arrays and remove not matched objects
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 say
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; }));
});
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line