如何将空元素推送到现有的 Js 数组,让我们假设:
var arr = [54,77,21];
var target = [54,77,21,,,,36];
arr.push(); //do not append an empty element into the array.
arr.push();
console.log(JSON.stringify(arr)); //output: [54,77,21]
如何附加空元素以便“arr”等同于“target”数组?
原文由 Voice Of The Rain 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以直接处理索引。这构建了一个稀疏数组。
或者推送
undefined
直到你喜欢推送该值。这将返回一个填充数组。By using
JSON.stringify
, you get for undefined or sparse itemsnull
, because JSON knows onlynull
instead ofundefined
.