JavaScript数组关联和索引? [英] javascript array associative AND indexed?

查看:115
本文介绍了JavaScript数组关联和索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JS数组是联想和索引?
我希望能够通过自己的立场或键值来查找阵列中的一个项目..
可能吗?

can an array in JS be associative AND indexed? I'd like to be able to lookup an item in the array by its position or a key value.. possible?

推荐答案

有没有这样的事情在Javascript关联数组。您可以使用对象文本,其中的像关联数组,但他们有无序性。定期的Javascript阵列基于整数索引,并且不能结合的。

There are no such things as associative arrays in Javascript. You can use object literals, which look like associative arrays, but they have unordered properties. Regular Javascript arrays are based on integer indexes, and can't be associative.

例如,这个对象:

var params = {
    foo: 1,
    bar: 0,
    other: 2
};

您可以从对象访问属性,例如:

You can access properties from the object, for example:

params["foo"];

你也可以使用迭代对象为中... 语句:

for(var v in params) {
    //v is equal to the currently iterated property
}

然而,财产迭代的顺序没有严格的规定 - 你的对象字面两次迭代可以按不同的顺序返回的属性。

However, there is no strict rule on the order of property iteration - two iterations of your object literal could return the properties in different orders.

这篇关于JavaScript数组关联和索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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