Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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"}
Comment

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);
Comment

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"}
Comment

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"}
Comment

javascript convert array to object

// Also, the following works well:
// IF the array came as follows (you cannot declare an array like that)
const obj = Object.assign({}, ['name': 'Alex', 'age': 43, 'gender': 'male']);
console.log(obj);
Comment

convert array to object

const names = ['Alex', 'Bob', 'Johny', 'Atta'];

const obj = Object.assign({}, names);
console.log(obj);
Comment

es6 convert array to object

const result = arr.reduce((obj, cur) => ({...obj, [cur.sid]: cur}), {})
Comment

Convert Array to Array Object Javascript

// from [1,2,3]
// to [{id:1},{id:2},{id:3}]

let data = ids.map((id)=>  ({id:id}))
Comment

js convert array to object

const names = ['Alex', 'Bob', 'Johny', 'Atta'];

const obj = Object.assign({}, names);
Comment

js array to object

const result = arr.reduce((obj, cur) => ({...obj, [cur.sid]: cur}), {})
Comment

PREVIOUS NEXT
Code Example
Javascript :: get current time epoch javascript 
Javascript :: truncate a string 
Javascript :: csurf npm 
Javascript :: desable no unused vars in vue.js 
Javascript :: add element to array javascript 
Javascript :: mocha timeout 
Javascript :: How to fix WordPress jQuery is not defined 
Javascript :: swap key value object javascript 
Javascript :: how to install react router dom 
Javascript :: prevstate in usestate 
Javascript :: mongoose query if field exists where filed exists 
Javascript :: queryselector 
Javascript :: regex domain 
Javascript :: error: expected undefined to be a graphql schema. 
Javascript :: iseet jquery 
Javascript :: node.js f string 
Javascript :: react fetch url 
Javascript :: js remove item from array by value 
Javascript :: remove class element 
Javascript :: js get random element in array 
Javascript :: document print from html javascript 
Javascript :: discordjs eval 
Javascript :: javascript return longest string in array 
Javascript :: copy text to clipboard javascript without input 
Javascript :: element ui handle enter key 
Javascript :: how to change the color of error message in jquery validation 
Javascript :: .call javascript 
Javascript :: clear swr cache 
Javascript :: json to list flutter 
Javascript :: docker react 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =