jquery 向数组中push 数据,如何引用变量?

黛曦葛溪 发布于 2016/09/18 14:35
阅读 1K+
收藏 0
configurations,currentConfig 分别是一个数组,selectIndex 是传递过来的参数(值是:1,2,3...),我想使用Push的方法得到以下的格式(后面要转成JSONG格式使用):

"configurations===========[
{"1":[{"type":"value","name":"UserIsPhone","value":"Disabled"},{"type":"value","name":"DNSmode","value":"Arecord"},{"type":"value","name":"LBCFramSize","value":"30ms"},{"type":"value","name":"SRTPMode","value":"Disabled"}]},

{"2":[{"type":"value","name":"UserIsPhone","value":"Disabled"},{"type":"value","name":"DNSmode","value":"Arecord"},{"type":"value","name":"LBCFramSize","value":"30ms"},{"type":"value","name":"SRTPMode","value":"Disabled"}]}
]"

当我使用configurations.push(selectIndex:currentConfig)的时候selectIndex 不能引用传递过来的参数,得到的格式是:

"configurations===========[
{"selectIndex":[{"type":"value","name":"UserIsPhone","value":"Disabled"},{"type":"value","name":"DNSmode","value":"Arecord"},{"type":"value","name":"LBCFramSize","value":"30ms"},{"type":"value","name":"SRTPMode","value":"Disabled"}]},

{"selectIndex":[{"type":"value","name":"UserIsPhone","value":"Disabled"},{"type":"value","name":"DNSmode","value":"Arecord"},{"type":"value","name":"LBCFramSize","value":"30ms"},{"type":"value","name":"SRTPMode","value":"Disabled"}]}
]"

而使用使用configurations.push("#{selectIndex}":currentConfig)却报语法错误:

SyntaxError: [stdin]:162:48: unexpected :

所以我怎么写才能正确引用到selectIndex呢?
加载中
0
mark35
mark35

js push()不能指定插入序列号吧。要不用对象

var configurations = {};

configurations[selectIndex] = currentConfig;

mark35
mark35
回复 @黛曦葛溪 : js的数组是稀疏数组,如果索引号非连续化容易造成空洞(极大)耗费资源。如果你循环出的selectIndex是顺序增长,那就直接用arr.push(data)
黛曦葛溪
黛曦葛溪
恩,后来使用对象了,只是很奇怪为什么不可以,有时候觉得代码有一些基础的应该支持就像java 的map 一样,key 跟value 都可以使用变量。
0
彼小星
彼小星
应该不能用对象的字面量,或许可以换成obj[selectIndex]=[{"type":"value","name":"UserIsPhone","value":"Disabled"},{"type":"value","name":"DNSmode","value":"Arecord"},{"type":"value","name":"LBCFramSize","value":"30ms"},{"type":"value","name":"SRTPMode","value":"Disabled"}]的形式
返回顶部
顶部