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

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

I have an array:

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


        
15条回答
  •  感动是毒
    2020-12-07 20:53

    ES5 Version using .reduce()

    const object = array.reduce(function(accumulatingObject, [key, value]) {
      accumulatingObject[key] = value;
      return accumulatingObject;
    }, {});
    

提交回复
热议问题