js array into object
const names = ['Alex', 'Bob', 'Johny', 'Atta']; // convert array to th object const obj = Object.assign({}, names); // print object console.log(obj); // {0: "Alex", 1: "Bob", 2: "Johny", 3: "Atta"}
Source: attacomsian.com
javascript object to array
//ES6 Object to Array const numbers = { one: 1, two: 2, }; console.log(Object.values(numbers)); // [ 1, 2 ] console.log(Object.entries(numbers)); // [ ['one', 1], ['two', 2] ]
Source: www.samanthaming.com
js array to object with keys
const arr = ['a','b','c']; const res = arr.reduce((a,b)=> (a[b]='',a),{}); console.log(res)
convert array to object in javascript
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);
Source: stackoverflow.com
javascript object to array
//Supposing fooObj to be an object fooArray = Object.entries(fooObj); fooArray.forEach(([key, value]) => { console.log(key); // 'one' console.log(value); // 1 })
Source: www.samanthaming.com
array to object
// array to object let data = [ {id: 1, country: 'Germany', population: 83623528}, {id: 2, country: 'Austria', population: 8975552}, {id: 3, country: 'Switzerland', population: 8616571} ]; let dictionary = Object.assign({}, ...data.map((x) => ({[x.id]: x.country}))); // {1: "Germany", 2: "Austria", 3: "Switzerland"}