【问题标题】:How to generate array key using array index in for on the associative array in javascript如何在javascript中的关联数组上使用数组索引生成数组键
【发布时间】: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


【解决方案1】:

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

简单示例:

let obj = {};

['a','b','c'].forEach((e,i) => {
  let computed = {[e]:i};
  Object.assign(obj, computed) 
})

console.log(obj)

【讨论】:

  • 非常小的建议,但 Array.reduce 更适合此解决方案
  • @Rajesh 给猫剥皮的多种方法
  • 因此以“非常轻微”开头
  • 谢谢,非常简单的代码msgObject.forEach((e, i) =&gt; { let columnIndex = { [e] : column[msgObject[i]] }; console.log(columnIndex); })
【解决方案2】:

这里有几种方法,请参阅我的 cmets。

  1. @charlietlf 建议的一种方法。
  2. 对象创建后,添加键/值
    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);
      }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    相关资源
    最近更新 更多