如何最好做一个JavaScript数组与非连续的索引? [英] How best to do a JavaScript array with non-consecutive indexes?

查看:123
本文介绍了如何最好做一个JavaScript数组与非连续的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个谷歌Chrome扩展,在JavaScript中,我想用一个数组来存储一堆的对象,但我想的指标是具体的非连续的ID号。

(这是因为我需要以后能够高效地查找值,使用来自我的控制之外的另一个源的ID号。)

例如:

  VAR myObjects = [];myObjects [471] = {foo的栏};myObjects [3119] = {打招呼:再见}

当我这样做的console.log(myObjects),在控制台中我看到了整个数组打印出来,所有的成千上万的丢失显示指标未定义

我的问题是:这件事情?难道这浪费任何记忆?

而且,即使它没有浪费内存,想必每当我环路阵列上,它浪费CPU,如果我必须手动跳过每一个缺失值?

我试图用一个对象而不是一个数组,但似乎你不能使用数字作为对象键。我希望有一个更好的方式来实现这一目标?


解决方案

首先,大家好,请知道什么为-in语句也被称为 枚举 的(虽然它是一个的 IterationStatement 的),以便从区分的迭代的。这是非常重要的,因为它会导致混乱,特别是中初学者。

要回答OP的问题:它不会占用更多的空间(测试)(你可以说这是实施相关的,但是我们现在谈论的一个的谷歌Chrome扩展!的),它是不是慢或者(测试)。

不过,我的建议是:使用什么是适当
在这种情况下:使用目标

你想与他们做的显然是一个散列机制,键转换为字符串反正这样你就可以安全地使用对象完成这个任务。

我不会让你有很多code的,其他的答案做它已经,我只想把事情说清楚。

  //键转换为字符串
//(号码可以安全地使用)
变种物镜= {}
OBJ [1] =值
警报(OBJ [1])//值
警报(OBJ [1])//值


  

请注意在稀疏数组


为什么稀疏数组的不会浪费任何空间的主要原因是因为该规范并没有这么说。没有地步,需要属性访问器来检查内部[[类]属性是一个的阵列的,然后创建从每个元素的 0℃; I< LEN 的是价值未定义等,它们恰好是未定义的toString 方法遍历数组。这基本上意味着它们不存在

11.2.1属性存取

生产的 MemberEx pression MemberEx pression 的[防爆pression 的]如下评价:


  1. 我们的 baseReference 的是评估的 MemberEx pression 的结果。

  2. 我们的 baseValue 的是的GetValue baseReference 的)。

  3. 我们的 propertyNameReference 的是评估的防爆pression 的结果。

  4. 我们的 propertyNameValue 的是的GetValue propertyNameReference 的)。

  5. 呼叫 CheckObjectCoercible baseValue 的)。

  6. 我们的 propertyNameString 的是的ToString propertyNameValue 的)。

  7. 如果说正在评估包含在严格模式code语法生产,让的的有真正,否则我们的的是的

  8. 返回类型的值参考的基值的 baseValue 的,其引用的名称是 propertyNameString 的,并且其严格模式标志的的。

生产的 CallEx pression CallEx pression 的[防爆pression 的]是完全一样的评价方式,所不同的是包含的 CallEx pression 的步骤进行评价1.

ECMA-262第5版 http://www.ecma-international.org/publications/standards/Ecma-262.htm)

I'm writing a Google Chrome extension, in JavaScript, and I want to use an array to store a bunch of objects, but I want the indexes to be specific non-consecutive ID numbers.

(This is because I need to be able to efficiently look up the values later, using an ID number that comes from another source outside my control.)

For example:

var myObjects = [] ;

myObjects[471] = {foo: "bar"} ;

myObjects[3119] = {hello: "goodbye"}

When I do console.log(myObjects), in the console I see the entire array printed out, with all the thousands of 'missing' indexes showing undefined.

My question is: does this matter? Is this wasting any memory?

And even if it's not wasting memory, surely whenever I loop over the array, it wastes CPU if I have to manually skip over every missing value?

I tried using an object instead of an array, but it seems you can't use numbers as object keys. I'm hoping there's a better way to achieve this?

解决方案

First of all, everyone, please learn that what the for-in statement does is called enumeration (though it's an IterationStatement) in order to differentiate from iteration. This is very important, because it leads to confusion especially among beginners.

To answer the OP's question: It doesn't take up more space (test) (you could say it's implementation dependent, but we're talking about a Google Chrome Extension!), and it isn't slower either (test).

Yet my advice is: Use what's appropriate! In this situation: use objects!

What you want to do with them is clearly a hashing mechanism, keys are converted to strings anyway so you can safely use object for this task.

I won't show you a lot of code, other answers do it already, I've just wanted to make things clear.

// keys are converted to strings 
// (numbers can be used safely)
var obj = {}
obj[1] = "value"
alert(obj[1])   // "value"
alert(obj["1"]) // "value"

Note on sparse arrays

The main reason why a sparse array will NOT waste any space is because the specification doesn't say so. There is no point where it would require property accessors to check if the internal [[Class]] property is an "Array", and then create every element from 0 < i < len to be the value undefined etc. They just happen to be undefined when the toString method is iterating over the array. It basically means they are not there.

11.2.1 Property Accessors

The production MemberExpression : MemberExpression [ Expression ] is evaluated as follows:

  1. Let baseReference be the result of evaluating MemberExpression.
  2. Let baseValue be GetValue(baseReference).
  3. Let propertyNameReference be the result of evaluating Expression.
  4. Let propertyNameValue be GetValue(propertyNameReference).
  5. Call CheckObjectCoercible(baseValue).
  6. Let propertyNameString be ToString(propertyNameValue).
  7. If the syntactic production that is being evaluated is contained in strict mode code, let strict be true, else let strict be false.
  8. Return a value of type Reference whose base value is baseValue and whose referenced name is propertyNameString, and whose strict mode flag is strict.

The production CallExpression : CallExpression [ Expression ] is evaluated in exactly the same manner, except that the contained CallExpression is evaluated in step 1.

ECMA-262 5th Edition (http://www.ecma-international.org/publications/standards/Ecma-262.htm)

这篇关于如何最好做一个JavaScript数组与非连续的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆