On the variable length of arr in js

topic description

Array in

JS is of variable length. If there is no defined value, it defaults to undefined

.

related codes

var arr = [0,1,2]; 
arr[10]=10;
arr.filter(function(x){
    return x===undefined
})

expect results

in the title, only arr is set to [0jue 1jue 2], and then set arr [10] = 10, at this time the arr should be equal to [0meme 1je 2je undefined 7 arr 10], and then Filter operation on this array, it will return an item equal to undefined, that is, the return should be [undefined*7]. But the result of the actual operation is an empty function [], and then output x, that is,

, inside the filter.
var arr = [0,1,2]; 
arr[10]=10;
arr.filter(function(x){
    console.log(x);
    return x===undefined
})

it is found that it will only output 0 ~ 1 ~ 2 ~ 10.

it seems that there is no explanation about this on MDN, so I hope God can help answer it. You will save a broken heart in the hot water. Thank you very much.

Jun.04,2021

directly set the array length is not included by several es5 ( Array.prototype.forEach , Array.prototype.map , Array.prototype.filter , Array.prototype.reduce ,.) Array iteration method loop. I didn't understand before
, but I saw their Polyfill in MDN , and iterated through the in operator to determine whether the index of the current iteration is in the array.
and directly set the length of the array index with no value directly because it cannot pass this judgment.


the rest of the elements are empty, not undefined filter map, and other methods skip empty elements directly

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3bab5-2c2cb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3bab5-2c2cb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?