Consider the following 2D array:
var fruits = [["Banana", "Orange", "Apple", "Pear"],
["Banana", "Orange", "Apple", "Pear"],
["Banana", "Orange", "Apple", "Pear"],
["Banana", "Orange", "Apple", "Pear"]];
What is the reason for the discrepancy in results between using map
and a for loop
?
var newArrayMap = fruits.map(f => f.splice(1,2));
console.log(newArray);
[["Orange", "Apple"], ["Orange", "Apple"], ["Orange", "Apple"], ["Orange", "Apple"]]
for (var i = fruits.length - 1; i >= 0; i--){
fruits[i].splice(1,2);
}
console.log(fruits);
[["Banana", "Pear"], ["Banana", "Pear"], ["Banana", "Pear"], ["Banana", "Pear"]]