[英]Set an item to empty in array Javascript
After initialing an array, it contains empty elements. 初始化数组后,它包含空元素。 I want to set an element back to empty after setting an value.
我想在设置值后将元素设置回空。 But it doesn't work however I set it to undefined/null.
但是它不起作用,但是我将其设置为undefined / null。
var a = Array(20181231); // it will not run. a.forEach(e => {console.log(++i + ' - ' + e);}); var i = 0; a[12] = 12; a[2018] = 2018; // it loop twice a.forEach(e => {console.log(++i + ' - ' + e);}); a[12] = undefined; // expecting one time, but it still loop twice. a.forEach(e => {console.log(++i + ' - ' + e);});
You could take the delete
operator and remove the element of the array. 您可以使用
delete
运算符并删除数组的元素。
var a = Array(20181231); a.forEach(e => console.log(++i + ' - ' + e)); // no loop var i = 0; a[12] = 12; a[2018] = 2018; a.forEach(e => console.log(++i + ' - ' + e)); // two elements delete a[12]; a.forEach(e => console.log(++i + ' - ' + e)); // one element
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.