本文介绍了稀疏数组的JavaScript中有哪些用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在稀疏数组比(常规)对象更好的情况下,您可能有什么编程用途?

What possible programming use could you have where a sparse array would be better than an (regular) object?

通过稀疏数组,我的意思是:

By sparse array I mean one where:

arr = [];                             //Initialize
arr[0] = 'W';
arr[1] = 'T';
arr[3] = 'F';

console.log(arr[0] !== undefined)    //true
console.log(arr[1] !== undefined)    //true
console.log(arr[2] === undefined)    //true
console.log(arr[3] !== undefined)    //true

或更正式:

An object, O, is said to be sparse if the following algorithm returns true:

1. Let len be the result of calling the [[Get]] internal method of O with argument
"length".

2. For each integer i in the range 0≤i<ToUint32(len)
    a. Let elem be the result of calling the [[GetOwnProperty]] internal method of O
    with argument ToString(i).

    b. If elem is undefined, return true.

3. Return false.

ECMA 262 5.1-15.4数组对象

此外,ECMA 262 5.1标准进一步将length定义为:

Moreover, the ECMA 262 5.1 Standard further defines length specifically as:

因此,尽管仅定义了三个元素,但上面的示例arr.length === 4.

So the example above, arr.length === 4 despite there only being three elements defined.

实际上,根据标准,任何大于3Number对于arr都是有效的length,包括Math.PI.

In fact, according to the standard, any Number greater than 3 is a valid length for arr, including Math.PI.

因此,这是否意味着没有人可以使用:

Consequently, does this mean that no one should use:

for(var i=0; i<arr.length; i++)
    //Cannot trust arr[i] exists

相反,使用它会更合适

for(key in arr)
    //Always exists

我从来没有在野外遇到过故意的人,实际上我只是在读这里奇怪的问答时才开始考虑它,现在我有点不安.

I've never encountered an intentional one in the wild, and really only began thinking about it while reading an odd Q&A here, and now I'm a little unsettled.

我早就知道,没有一种巧妙的方法可以从Array中删除元素,但是现在我更加困惑为什么您会故意留下一个孔,更不用说定义长度可以大于最后定义的元素的任何数字.

I've long known that there's not a neat way to remove an element from an Array, but now I'm even more confused as to why you would intentionally leave a hole, let alone define a standard where length can be any number greater than the last defined element.

如果我想要随机键值对,则可以使用Object.如果我希望能够完全迭代,请使用Array.我想念什么吗?

If I wanted random key value pairs, I'd use an Object. If I want to be able to cleanly iterate, I use an Array. Am I missing something?

注意,我正在寻找一个特定的用例,或一类通用的用例,而不是对标准的引用或意见.我知道这是允许的,并且我已经有了意见. :)

Note, I'm looking for a specific use case, or a class of generalized use cases not a reference to the standards, or an opinion. I know it's allowed, and I already have an opinion. :)

要添加测试或查看一些我已经看到的Array标准以意外方式工作的测试,请查看此小提琴

To add a test or see some of the ones I've seen where the Array standard works in unexpected ways, check out this fiddle

很抱歉,如果这有点抽象.整夜都在想这件事.

Sorry if this is a bit abstract. Been thinking about it all night.

推荐答案

我在实际使用中遇到的稀疏数组的一个可能用例是热图.

One possible use-case for sparse arrays that I've come across in real usage is for a heat-map.

从您的地图开始是X×的空数组; Y元素.加载数据,然后通过增加相关坐标处的数组元素将其填充到地图中.

Start with your map being an empty array of X × Y elements. Load your data, and populate it into the map by incrementing the array elements at the relevant co-ords.

另一个类似的例子可能是战舰游戏,通过在适当的坐标处填充数组元素,将船只放置在一个空网格中.

Another similar example might be a battleship game, where boats are placed into an empty grid by populating the array elements at the appropriate co-ordinates.

这并不是说这是这样做的唯一方法,或者甚至是最好的方法-这两个示例都可以很容易地实现而无需使用稀疏数组-但问题是需要用例,所以您就可以了

That's not to say this is this only way to do this, or even the best way -- both examples can quite easily be achieved without using a sparse array -- but the question was asking for use cases, so there you go.

这篇关于稀疏数组的JavaScript中有哪些用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:30