let arr = [1, "5", 3, 27, undefined, { name: 'Steven' }, 11];
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] !== 'number') {
arr.splice(i, 1);
}
}
console.log(arr); // result: [1, 3, 27, {…}, 11]
When switching the position of the object and the last number in the array, the output changes.
let arr = [1, "5", 3, 27, undefined, 11, { name: 'Steven' }];
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] !== 'number') {
arr.splice(i, 1);
}
}
console.log(arr); // result: [1, 3, 27, 11]
Could someone provide an explanation for this?