【发布时间】:2020-12-06 05:27:55
【问题描述】:
是否可以使用“for”中的数组索引生成数组键来创建关联数组?
我希望将“for”中的索引数组中的值用作关联数组中的键
你想要获取值来制作关联数组的示例代码在***符号的中间:
if(data.status == 422){
let msg = data.responseJSON.errors;
let msgObject = Object.keys(msg);
for (let i = 0; i < msgObject.length; i++) {
if (msg[msgObject[i]]) {
let msgObjectIndex = msgObject[i];
let columnIndex = {
||
\/
***msgObject[i]*** : column[msgObject[i]]
/\
||
};
console.log(columnIndex);
}
}
}else {
alert('Please Reload to read Ajax');
console.log("ERROR : ", e);
}
},
那么变量列是:
let column = {
'nama_paket' : $('.edit_error_nama_paket'),
'tipe_berat' : $('.edit_error_tipe_berat'),
'harga' : $('.edit_error_harga'),
'id_service' : $('.edit_error_id_service'),
};
我尝试上面的代码得到以下错误: Uncaught SyntaxError: Unexpected token '[' 谢谢
【问题讨论】:
-
columnIndex的声明/赋值中发生了什么?那肯定是非法的语法。这只是作为这个问题的提示吗?如果是这样,最好只添加一个注释,以便您的示例代码中的语法清晰正确。 -
在你这部分代码
column[msgObject[i]]中,column是什么意思? -
我编辑了帖子,关于变量“列”
-
你可以通过surrounding it in square brackets:
columnIndex={ [ msgObject[i] ] : column[msgObject[i]]}来初始化一个具有计算属性名称的对象。 -
是的,这是非法语法,我只想知道如何获取值“msgObject [i]”(位于 *** 符号的中间),然后将该值用作数组键
标签: javascript jquery arrays ajax associative-array