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

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

I have an array:

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


        
15条回答
  •  一整个雨季
    2020-12-07 20:30

    When I used the reduce function with acc[i] = cur; it returned a kind of object that I needed to access it like a array using this way obj[i].property. But using this way I have the Object that I wanted and I now can access it like obj.property.

     function convertArraytoObject(arr) {
            var obj = arr.reduce(function (acc, cur, i) {
                acc = cur;
                return acc;
            }, {});
            return obj;
        }
    

提交回复
热议问题