There are a few issues. There is a trailing comma in the myArray array literal that is incorrectly treated as an elision in IE, so the array has a length that is too long. Also, anotherArray includes all the members of myArray, so if the function works, it removes all of them.
You can use splice to remove members from an array. It affects the length and in an incrementing loop means you'll skip the next member if you don't modify the counter. The usual strategy is to use a decrementing loop to avoid those issues.
Lastly, the "name" property is a string but the values in anotherArray are numbers, which might mess with the comparison.
myArray = [
{ name: "1",
age: "20"
},
{ name: "6",
age: "20"
},
{ name: "2",
age: "20"
} // trailing comma removed
];
anotherArray = [2,1,3,4,5]; // 6 removed so one member remains
for (var i=myArray.length - 1; i >= 0; i--) {
// Convert name to a number since indexOf uses ===
if (anotherArray.indexOf(+myArray[i].name) != -1) {
myArray.splice(i, 1);
}
}
console.log(myArray[0].name);
A polyfill for indexOf can be found at MDN.