let a = Array(3);
a[0] = 1;
a[1] = undefined;
function test(arr) {
return arr.map(a => !!a);
}
console.log('before', a); // [1, undefined, undefined]
console.log('after', test(a)); // [true, false, undefined]
如何檢查陣列元素是初始化為未定義(a[1])還是未初始化(a[2])?a[2] 具有空值,但瀏覽器將其轉換為未定義。
uj5u.com熱心網友回復:
您可以hasOwnProperty
與索引一起使用。
當索引處沒有設定值時,hasOwnProperty
回傳false
:
const a = Array(3);
a[1] = undefined;
console.log(a.hasOwnProperty(0));
console.log(a.hasOwnProperty(1));
console.log(a.hasOwnProperty(2));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/493391.html
標籤:javascript 变量 类型 不明确的