I have an array:
[ [ \'cardType\', \'iDEBIT\' ],
[ \'txnAmount\', \'17.64\' ],
[ \'txnId\', \'20181\' ],
[ \'txnType\', \'Purchase\' ],
[ \'txnDate\
I much more recommend you to use ES6 with it's perfect Object.assign()
method.
Object.assign({}, ...array.map(([ key, value ]) => ({ [key]: value })));
What happening here - Object.assign()
do nothing but take key:value from donating object and puts pair in your result. In this case I'm using ...
to split new array
to multiply pairs (after map it looks like [{'cardType':'iDEBIT'}, ... ]
). So in the end, new {}
receives every key:property from each pair from mapped array
.
ECMAScript 2021 brings Object.fromEntries which does exactly the requirement:
const array = [ [ 'cardType', 'iDEBIT' ],
[ 'txnAmount', '17.64' ],
[ 'txnId', '20181' ],
[ 'txnType', 'Purchase' ],
[ 'txnDate', '2015/08/13 21:50:04' ],
[ 'respCode', '0' ],
[ 'isoCode', '0' ],
[ 'authCode', '' ],
[ 'acquirerInvoice', '0' ],
[ 'message', '' ],
[ 'isComplete', 'true' ],
[ 'isTimeout', 'false' ] ];
const obj = Object.fromEntries(array);
console.log(obj);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries
This will do it:
const array = [ [ 'cardType', 'iDEBIT' ],
[ 'txnAmount', '17.64' ],
[ 'txnId', '20181' ],
[ 'txnType', 'Purchase' ],
[ 'txnDate', '2015/08/13 21:50:04' ],
[ 'respCode', '0' ],
[ 'isoCode', '0' ],
[ 'authCode', '' ],
[ 'acquirerInvoice', '0' ],
[ 'message', '' ],
[ 'isComplete', 'true' ],
[ 'isTimeout', 'false' ] ];
var obj = {};
array.forEach(function(data){
obj[data[0]] = data[1]
});
console.log(obj);
Use Map.
new Map(array);
The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.
This works because the type of your variable array
is Array<[key,value]>
. The Map
constructor can be initialized with an array of arrays where the first element of the inner arrays is the key and the second is the value.
const array = [
['cardType', 'iDEBIT'],
['txnAmount', '17.64'],
['txnId', '20181'],
['txnType', 'Purchase'],
['txnDate', '2015/08/13 21:50:04'],
['respCode', '0'],
['isoCode', '0'],
['authCode', ''],
['acquirerInvoice', '0'],
['message', ''],
['isComplete', 'true'],
['isTimeout', 'false']
];
const obj = new Map(array);
console.log(obj.get('txnDate'));
Update 2020: As baao notes, Object.fromEntries(arr)
now does this on all modern browsers.
You can use Object.assign, the spread operator, and destructuring assignment for an approach that uses map
instead of @royhowie’s reduce, which may or may not be more intuitive:
Object.assign(...arr.map(([key, val]) => ({[key]: val})))
E.g.:
var arr = [ [ 'cardType', 'iDEBIT' ],
[ 'txnAmount', '17.64' ],
[ 'txnId', '20181' ],
[ 'txnType', 'Purchase' ],
[ 'txnDate', '2015/08/13 21:50:04' ],
[ 'respCode', '0' ],
[ 'isoCode', '0' ],
[ 'authCode', '' ],
[ 'acquirerInvoice', '0' ],
[ 'message', '' ],
[ 'isComplete', 'true' ],
[ 'isTimeout', 'false' ] ]
var obj = Object.assign(...arr.map(([key, val]) => ({[key]: val})))
console.log(obj)
The new JS API for this is Object.fromEntries(array of tuples)
, it works with raw arrays and/or Maps
You could do this easily using array reduce in ES6
In this example we create a reducer function and pass an object '{}' as initial value to the reduce function along with the reducer
const arr = [ [ 'cardType', 'iDEBIT' ],
[ 'txnAmount', '17.64' ],
[ 'txnId', '20181' ],
[ 'txnType', 'Purchase' ],
[ 'txnDate', '2015/08/13 21:50:04' ],
[ 'respCode', '0' ],
[ 'isoCode', '0' ],
[ 'authCode', '' ],
[ 'acquirerInvoice', '0' ],
[ 'message', '' ],
[ 'isComplete', 'true' ],
[ 'isTimeout', 'false' ] ];
const reducer = (obj, item) => {
obj[item[0]] = item[1];
return obj;
};
const result = arr.reduce(reducer, {});
console.log(result);