How I can convert array to object in Javascript

javascript convert array to object with keys
convert array of objects to object javascript
convert array to object javascript es6
map = array to object javascript
convert array to object javascript lodash
convert array to object jquery
convert array to object php
javascript convert array to object without key

I am trying to convert in Javascript an array

A=['"age":"20"','"name":"John"','"email":"john@email.com"'];

to object

O={"age":"20","name":"John","email":"john@email.com"}.

How I can do this. Thanks

Should be straight forward, just iterate and split on the colon

var A = ['"age":"20"','"name":"John"','"email":"john@email.com"'];

var O = {};

A.forEach(function(item) {
    var parts = item.split(':').map(function(x) { return x.trim().replace(/\"/g,'') });
    
    O[parts[0]] = parts[1];
});

document.body.innerHTML = '<pre>' + JSON.stringify(O, null, 4) + '</pre>';

Convert Array to Object, In modern JavaScript runtimes, you can use the .reduce() method: var obj = arr. reduce(function� To convert an array into an object we will create a function and give it 2 properties, an array and a key. const convertArrayToObject = (array, key) => {}; We will then reduce the array, and create a unique property for each item based on the key we have passed in.

Since the keys are quoted, you can take advantage of JSON.parse. You can just make the array a string, wrap it in curly brackets, and parse it.

var A = ['"age":"20"', '"name":"John"', '"email":"john@email.com"'];

var temp = "{" + A.toString() + "}";
var theObj = JSON.parse(temp);
console.log(theObj);

Convert an Array to an Object in JavaScript, Example-1: This example converts the array to object by using Object.assign() method. For display it, JSON.stringify() method is used. To convert an array into an object we will create a function and give it 2 properties, an array and a key. const convertArrayToObject = (array, key) => {}; We will then reduce the array, and create a unique property for each item based on the key we have passed in.

Try this:

const A = ['"age":"20"', '"name":"John"', '"email":"john@email.com"'];
const result = A.reduce((res, i) => {
    let s = i.split(':');
    return {...res, [s[0]]: s[1].trim().replace(/\"/g, '')};
}, {});
console.log(result);

How to convert an array to an object in JavaScript, How to convert an array to an object in JavaScript You can also add a function to Array 's prototype and call it whenever you want to convert� Just want to point out - if you already have an array of sorted properties from the original object, using the spread operator is what will turn that array directly into a new object: { [sortedArray]} – HappyHands31 Jul 26 '19 at 15:18

Rewriting JavaScript: Converting an Array of Objects to an Object , For me the ideal solution would be to convert the array into an object that looks like the following: const peopleObject = { "123": { id: 123, name:� Convert an Array to an Object. Previously, converting from an object to an array was the only direction with built-in methods for the Object class. However as of ES2019/ES10, the .fromEntries

Object.fromEntries(), Converting an Array to an Object. With Object.fromEntries , you can convert from Array to Object : const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]; const obj = Object. Approach: We will use Object.entries () which is available in JavaScript. Object.entries () method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually.

Array.from(), arrayLike: An array-like or iterable object to convert to an array. the intermediate array would necessarily have values truncated to fit into the� So, in this article, I will let you know how can you convert a JavaScript array to an object with example JS coding. The easiest way of converting an array into a usable object is to use the spread operator (…) before the array. Using this operator we can expand our array expression in places where zero or more array elements are expected.

Comments
  • What did you try so far?
  • possible duplicate of Convert Array to Object
  • I tried : var myJsonString = JSON.stringify(A); var obj = JSON.parse(myJsonString); console.log(obj);
  • Thanks Adeneo for your quick answer, but the object have an additional '' , exemple : '"key"':'"value"'
  • Well, that's because you have quotes inside quotes, I've removed them for you.
  • That's clever, +1, didnt' think of that, but it does assume everything is always quoted with double quotes and is valid JSON when stringified.
  • Thanks for this solution aswell