简体   繁体   English

jQuery从索引获取关联数组键

[英]jQuery Get associative array Key from index

My question is the following, in an array: 我的问题如下,在一个数组中:

    var array = new Array();
    array['abc'] = 'value1';
    array['def'] = 'value2';

How do I get the associative key of an array if I have its index number? 如果我有索引号,如何获取数组的关联键? Let's say I want associative key of arr[0]'s associative key ( => 'abc'), or associative key of arr[1] '=> 'def'). 假设我想要arr [0]的关联键(=>'abc')的关联键,或arr [1]'=>'def'的关联键。 How is this possible in jQuery? 这怎么可能在jQuery中?

Let's be clear, I am not looking for the value and I do not need to use $.each(). 我们要清楚,我不是在寻找价值而且我不需要使用$ .each()。 I just need to link 0 to 'abc' and 1 => 'def' etc... Unfortunately something like arr[0].assoc_key() doesn't seem to exist T_T Thanks a bunch. 我只需要将0链接到'abc'和1 =>'def'等...不幸的是像arr [0] .assoc_key()似乎不存在T_T感谢一堆。

All right so the solution is pretty simple, you need to create an object which associates indeces with keys as well as keys with values. 好吧所以解决方案非常简单,你需要创建一个对象,它将indeces与键以及带有值的键相关联。 Here is a JSBin that works. 这是一个有效的JSBin。 Please note that to add an element, you need a custom function (addElement in this case) to be able to have both indeces and keys associated at the right places. 请注意,要添加元素,您需要一个自定义函数(在本例中为addElement),以便能够在正确的位置关联indeces和keys。 This is a rough draft to give you an idea of how it can be done! 这是一个粗略的草案,让您了解如何做到这一点!

JSBin JSBin

If you have any question or if that wasn't exactly what you expected, simply edit your question and I'll have another glance at it. 如果您有任何问题,或者这不是您所期望的,只需编辑您的问题,我就可以再看一眼。 It HAS to be a custom made object if you want the behavior you asked for. 如果你想要你想要的行为,它必须是一个自定义对象。

Javascript doesn't have a native Dictionary type, you would have to write it. Javascript没有本机字典类型,您必须编写它。 – T McKeown - T McKeown

It isn't possible and jQuery doesn't come into the picture at all. 这是不可能的,jQuery根本没有进入图片。 If you use an array as a dictionary like that, you are doing something wrong. 如果你使用数组作为这样的字典,你做错了。 – Jon - 乔恩

rethink the way you are doing it. 重新思考你的方式。 Maybe try array[0] = {key: 'abc', value: 'value1'} – Geezer68 也许尝试array [0] = {key:'abc',value:'value1'} - Geezer68

@Geezer68, objects do not support multidimentional data, the array I'm working on is 3 levels deep (I know I didn't says so in my original post, but I didn't think it was relevant). @ Geezer68,对象不支持多维数据,我正在处理的数组是3级深度(我知道我在原帖中没有这么说,但我认为它不相关)。

Anyway, thank you guys, it answers the question! 无论如何,谢谢你们,它回答了这个问题! I will rethink it then ;-) 我会重新考虑一下;-)

EDIT: I guess I'll just add a level: 编辑:我想我只会添加一个级别:

    var array = new Array();
    array[] = 'abc';
    array[0] = 'value1'

I don't know an other than using a for ... in . 我不知道使用for ... in So here how i do it and hope you get a better answer (because i want to know aswell!). 所以在这里我是如何做到的,并希望你得到一个更好的答案(因为我想知道!)。

var array = new Array();
array['abc'] = 'value1';
array['def'] = 'value2';

var listKeys = [];
for(x in array) listKeys.push(x);

console.log(listKeys); // ['abc', 'def']

but using [string] on an array object is adding property to the object, not the array. 但在数组对象上使用[string]是向对象添加属性,而不是数组。 So it may be better to initialise it like that : 所以最好像这样初始化它:

var array = {};

You might learn more information on this technique in this question and some restriction on why you should not rely on that. 您可以在此问题中了解有关此技术的更多信息,以及对不应依赖于此的一些限制。

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

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