Suppose I am a user script developer and I don't have control over the on-page javascript. The page creates arrays with random lengths, filling them with random values (including falsy ones, such as undefined
). Not every element has to be assigned a value, so there may be empty slots.
A simplified example (Firefox console):
var arr = new Array(3);
arr[0] = null;
arr[1] = undefined;
console.log(arr); \ Array [ null, undefined, <1 empty slot> ]
console.log(arr[1]); \ undefined
console.log(arr[2]); \ undefined
console.log(arr[1] === arr[2]); \ true
console.log(typeof arr[1]); \ undefined
console.log(typeof arr[2]); \ undefined
As we can see, Firefox displays undefined
and empty slots differently, whereas for javascript they seem to be identical.
Now suppose I want to clean such an array, removing all empty slots but leaving undefined
elements intact. How do I do that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…