How to convert an array of key-value tuples into an object

后端 未结 15 1908
南方客
南方客 2020-12-07 20:11

I have an array:

[ [ \'cardType\', \'iDEBIT\' ],
  [ \'txnAmount\', \'17.64\' ],
  [ \'txnId\', \'20181\' ],
  [ \'txnType\', \'Purchase\' ],
  [ \'txnDate\         


        
15条回答
  •  旧时难觅i
    2020-12-07 20:27

    use the following way to convert the array to an object easily.

    var obj = {};
    array.forEach(function(e){
       obj[e[0]] = e[1]
    })
    

    This will use the first element as the key and the second element as the value for each element.

提交回复
热议问题