简体   繁体   English

可以将空插槽推入阵列吗?

[英]Possible to push empty slot to an array?

I'm building my own map method to be as close as the native map method. 我正在建立自己的地图方法,使其与本地地图方法一样接近。 Since the native map pushes(i think) the changed values into a new array, it still keeps the empty slots. 由于本机映射将(我认为)更改后的值压入一个新数组,因此它仍然保留空白插槽。 I wasn't able to find a solution to push an empty slot into an array, like this example below. 我无法找到将空插槽推入阵列的解决方案,如下面的示例所示。

[1, 2, 3].push(some code) // [1, 2, 3, empty]

I tried pushing an array with one empty item prefixed with a spread operator arr.push(...(new Array(1))) or arr.push(...[,]) but that just pushes undefined . 我尝试推送一个数组,该数组的一个空项目的前缀为散布运算符arr.push(...(new Array(1)))arr.push(...[,])但这只是推送undefined

I solved my problem by not using push and instead assigning values to the array index that way skipped indices will be set to empty. 我通过不使用push来解决问题 ,而是将值分配给数组索引,这样跳过的索引将设置为空。

But I'm writing this post to see if anyone knows that if it's possible to use the push method to push an empty slot to an array. 但是我正在写这篇文章,以查看是否有人知道是否可以使用push方法将空插槽推入数组。

No, it's not possible, not with the push method. 不,这是不可能的,不是push方法。 empty can only exist if the array has a certain length, but a whole number property of the array does not exist at some index. 仅当数组具有特定长度时才可以存在empty ,但是数组的整数属性在某个索引处不存在。 This is called a sparse array , and cannot be created with push (or other array methods, if they're called on and with non-sparse arrays). 这称为稀疏数组 ,不能通过push (或其他数组方法,如果它们是在非稀疏数组上调用的话)创建的。

The only way to do so would be to assign to an index for which a lower index doesn't exist yet. 这样做的唯一方法是将其分配给尚不存在较低索引的索引。

Look at the results for the below two snippets in your browser console, not the snippet console: 在浏览器控制台而不是代码段控制台中查看以下两个代码段的结果:

 const arr = []; arr[1] = 'a'; console.log(arr); 

Or to set the .length of the array above the last index that the array has: 或将数组的.length设置为该数组具有的最后一个索引上方:

 const arr = []; arr.length = 1; console.log(arr); 

But the two approaches above are very weird to do and probably have no good reason to be used. 但是上述两种方法非常奇怪 ,可能没有充分的理由使用。 Better to avoid sparse arrays entirely. 最好完全避免稀疏数组。

Keep in mind that an empty slot is different from undefined , which is perfectly possible to have as an array value: 请记住,空插槽不同于undefined ,它很可能具有数组值:

 const arr = []; arr.push(undefined); console.log(arr); 

You can create an empty slot in an array by incrementing the array length: 您可以通过增加数组长度在数组中创建一个空插槽:

 var a = [] a.push(1) a.length++ a.push(3) console.log(a) console.log(1 in a) // anything at index 1? 

Alternatively, you can push something and then delete it: 或者,您可以推送某些内容然后将其删除:

 var a = [] a.push(1) a.push(2) a.push(3) delete a[1] console.log(a) console.log(1 in a) // anything at index 1? 

There is no need to actually push to a new array in your implementation. 实际上,您无需在实现中推送到新数组。 You can simply do new Array(this.length) where this.length is the array you are mapping through length. 您可以简单地执行new Array(this.length) ,其中this.length是要通过长度映射的数组。

For example consider this map implementation: 例如,考虑以下map实现:

 if (!Array.prototype.mapIt) { Object.defineProperty(Array.prototype, "mapIt", { value: function(fn) { if (this === null) { throw new TypeError('Array.prototype.mapIt called on null or undefined'); } if (typeof fn !== 'function') { throw new TypeError('predicate must be a function'); } let _array = this.filter(x => x != null) // remove empty values let result = new Array(_array.length) // the new array we will return for (var i = 0; i < _array.length; i++) { result[i] = fn.call(arguments[1], _array[i], i, _array) // call the predicate } return result; } }); } let arr = [1, 2, , , 3] // the test array let result = arr.mapIt((c, i, a) => console.log(`current: ${c}`, `index: ${i}`, `array: ${a}`) || c + 2) console.log('result: ', result) console.log('original array: ', arr) 

Hope this helps you with an gives you an idea about a possible map implementation. 希望这对您有所帮助,让您对可能的map实现有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM