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"}
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"}
array of objects to object
const toObject = (arr, key) => arr.reduce((a, b) => ({ ...a, [b[key]]: b }), {});
// Example
toObject(
[
{ id: '1', name: 'June', gender: 'Female' },
{ id: '2', name: 'Alex', gender: 'Male' },
{ id: '3', name: 'Harry', gender: 'Male' },
],
'id'
);
/*
{
'1': { id: '1', name: 'June', gender: 'Female' },
'2': { id: '2', name: 'Alex', gender: 'Male' },
'3': { id: '3', name: 'Harry', gender: 'Male' },
}
*/
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);
javascript convert array to object
function arrayToObject(arr) {
var obj = {};
for (var i = 0; i < arr.length; ++i){
obj[i] = arr[i];
}
return obj;
}
var colors=["red","blue","green"];
var colorsObj=arrayToObject(colors);//{0: "red", 1: "blue", 2: "green"}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us