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

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 :: find only duplicate data javascript object 
Javascript :: exceljs read file 
Javascript :: javascript range 
Javascript :: js submit 
Javascript :: javascript array random selector 
Javascript :: GET req with js 
Javascript :: set css variable from javascript 
Javascript :: list methods of object js 
Javascript :: how to reload a module in node.js 
Javascript :: find class using jquery 
Javascript :: jquery checkbox 
Javascript :: first letter tuUppercase 
Javascript :: discord js convert timestamp to date 
Javascript :: popin localstorage once 
Javascript :: register a service worker 
Javascript :: best javascript ide 
Javascript :: javascript every other element in array 
Javascript :: next js script tag 
Javascript :: compare and filter two array of object 
Javascript :: if select option disabled jquerz 
Javascript :: bodyparser purpose 
Javascript :: input length material Ui Design 
Javascript :: string repeat codewars javascript 
Javascript :: jquery check if type is checkbox 
Javascript :: (0 , _reactRouterDom.useHistory) is not a function 
Javascript :: Deleting all white spaces in a string 
Javascript :: js array find string element with max length 
Javascript :: how to fetch the all input element id value 
Javascript :: postman test check response status 
Javascript :: ReferenceError: Buffer is not defined 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =