jQuery创建具有动态键和多个值的关联数组 [英] Jquery creating associative array with dynamic keys and multiple values

查看:280
本文介绍了jQuery创建具有动态键和多个值的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建以下内容:

array('12345'=>'A01','A02','A03'
'22222'=>'B01',
'33333'=>'C01','C02')

array( '12345' => 'A01', 'A02', 'A03'
'22222' => 'B01',
'33333' => 'C01', 'C02')

因此,基本上每个键都是从另一个数组动态生成的不同键.假设触发特定事件后变量号为"12345".

So basically each key is different generated dynamically from another array. Let's say variable numbers has '12345' after certain event is fired.

我们有一个名为location的数组,该数组将具有例如('A01','A02','A03')

We have an array called location, this one will have for example ('A01', 'A02', 'A03')

因此,然后在主阵列上,它将两个数字都映射到位置.这是每次发生事件时都需要保存的数组.

So then on a master array it will map both numbers with the location. This is the array that i will need to save each time there is an event.

在下一个事件执行时,我们将在数字变量上获得一个新值,例如'22222',然后新的数组位置将用('B01')覆盖另一个值,依此类推.

The on the next event execution we shall get a new value on numbers variable such as '22222' and then a new array location will overwrite the other one with ('B01') for example and so on.

请记住,键将始终是动态的,并且值可以在1到50之间,例如我们不知道的.我知道这更像是Jquery上的对象文字.提前谢谢.

Remember the keys are gonna be always dynamic and the values can be from 1 to 50 for example we don't know. I know this is more like Object Literals on Jquery. thx in advance.

这是一段代码,需要能够获取键和值

Here is the piece of code, need to be able to get the key and values

             $.each(dragarray, function(index, value) {

                    dragid_loc['value'] = [];
                    // do loop to add each element of other array
                    $.each(draglocation, function(index2, value2) {
                        dragid_loc.value.push(value2);
                    });

            });

            console.log(dragid_loc);

这行似乎引起了问题,我不会将另一个数组draglocation的值推入每个数组中.需要获取键和值.

This line seems to cause the problem i won't push the values of another array draglocation into each. Need to get the key and the value.

dragid_loc.value.push(value2);

推荐答案

基于这些评论,我认为您需要的是:

Based in the comments I think what you need is:

  • obj["newProp"] = []; // A new property is added to the object with key newProp and an empty array as value
  • obj.newProp.push(newElement); // A new element is added to the array in newProp of object
  • obj["newProp"] = []; // A new property is added to the object with key newProp and an empty array as value
  • obj.newProp.push(newElement); // A new element is added to the array in newProp of object

这篇关于jQuery创建具有动态键和多个值的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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