Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript的关联数组中使用数组索引生成数组键_Javascript_Jquery_Arrays_Ajax_Associative Array - Fatal编程技术网

如何在javascript的关联数组中使用数组索引生成数组键

如何在javascript的关联数组中使用数组索引生成数组键,javascript,jquery,arrays,ajax,associative-array,Javascript,Jquery,Arrays,Ajax,Associative Array,是否可以使用“for”中的数组索引生成数组键以创建关联数组 我希望“for”中索引数组中的值用作关联数组中的键 希望获得关联数组的值的示例代码位于**符号的中间: if(data.status == 422){ let msg = data.responseJSON.errors; let msgObject = Object.keys(msg);

是否可以使用“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);
                    }
                },
我尝试了上面的代码以获得以下错误:uncaughtsyntaxerror:意外的令牌“[”
谢谢

您可以使用
[]
语法生成计算属性名称

简单的例子:

让obj={};
[a','b','c'].forEach((e,i)=>{
设computed={[e]:i};
分配对象(对象,计算)
})

console.log(obj)
这里有几种方法可以做,请参阅我的评论

  • 一种方法是@charlietlf建议的
  • 创建对象后,添加键/值
  • let msg=data.responseJSON.errors;
    让msgObject=Object.keys(msg);
    for(设i=0;i
    columnIndex
    的声明/赋值中发生了什么?这肯定是非法语法。这只是这个问题的一个提示吗?如果是这样,最好在示例代码中添加一条注释,以便语法清晰正确。在代码的这一部分
    column[msgObject[i]]< >代码>什么意思?我编辑了这个帖子,关于变量“列”,你可以用一个计算属性名初始化一个对象:<代码> CalnQueals= {[MSGObjtu[i] ]:列[MSGOBBAST[i] ] } /COD>。是的,这是非法语法,我只想知道如何获得“MSGOBBAST[i]”的值(在**符号的中间)然后将该值用作数组键非常小的建议,但array.reduce适合此解决方案better@Rajesh很多方法都是从“非常小”开始的,谢谢,非常简单的代码
    msgObject.forEach((e,i)=>{let columnIndex={[e]:column[msgObject[i]};console.log(columnIndex);})
    let column = {
                'nama_paket' : $('.edit_error_nama_paket'), 
                'tipe_berat' : $('.edit_error_tipe_berat'), 
                'harga'      : $('.edit_error_harga'), 
                'id_service' : $('.edit_error_id_service'),
            };
    
        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];
        
            const newKey1 = `MyNewKey${2 + 3}`; // sample
            let columnIndex = {
              // One way is
              [newKey1]: column[msgObject[i]],
            };
        
            // Alternatively, we can add the generated key and value to obj
            const newKey2 = msgObject[i];
            //  or something like newKey2 = `MyKey${msgObject[i]}`
            columnIndex[newKey2] = column[msgObject[i]];
        
            console.log(columnIndex);
          }
        }