'Destructure array to object property keys
I have an array of values like:
const arr = [1,2,3];
Is there any way I can use destructuring to create the following output? If not, what is the easiest way I can do this in ES6 (or later)?
const obj = {
one: 1,
two: 2,
three: 3
};
I tried this, but I guess it doesn't work as this is the syntax for computed keys:
const arr = [1,2,3];
const obj = {
[one, two, three] = arr
};
Solution 1:[1]
You can assign destructured values not only to variables but also to existing objects:
const arr = [1,2,3], o = {};
({0:o.one, 1:o.two, 2:o.three} = arr);
This works without any additional variables and is less repetitive. However, it also requires two steps, if you are very particular about it.
Solution 2:[2]
With destructuring, you can either create new variables or assign to existing variables/properties. You can't declare and reassign in the same statement, however.
const arr = [1, 2, 3],
obj = {};
[obj.one, obj.two, obj.three] = arr;
console.log(obj);
// { one: 1, two: 2, three: 3 }
Solution 3:[3]
Using destructuring assignment it is possible to assign to an object from an array
Please try this example:
const numbers = {};
[numbers.one, numbers.two, numbers.three] = [1, 2, 3]
console.log(numbers)
The credit to the boys of http://javascript.info/ where I found a similar example. This example is located at http://javascript.info/destructuring-assignment in the Assign to anything at the left-side section
Solution 4:[4]
This answers a slightly different requirement, but I came here looking for an answer to that need and perhaps this will help others in a similar situation.
Given an array of strings : a = ['one', 'two', 'three'] What is a nice un-nested non-loop way of getting this resulting dictionary: b = { one : 'one', two: 'two', three: 'three' } ?
const b = a.map(a=>({ [a]: a })).reduce((p, n)=>({ ...p, ...n }),{})
Solution 5:[5]
You can achieve it pretty easily using lodash's _.zipObject
const obj = _.zipObject(['one','two','three'], [1, 2, 3]);
console.log(obj); // { one: 1, two: 2, three: 3 }
Solution 6:[6]
let distructingNames = ['alu', 'bob', 'alice', 'truce', 'truce', 'truce', 'truce', 'bob'];
let obj={};
distructingNames.forEach((ele,i)=>{
obj[i]=ele;
})
console.log('obj', obj)
Solution 7:[7]
One of the easiest and less code way is to destructure the array. Then use such constants to update the object.
const arr = [1, 2, 3];
const [one, two, three] = arr;
const obj = {one, two, three};
console.log(obj);
Notice how I assigned values to the object by such writing the names of the constants one, two, and three. You can do so when the name of the key is the same of the property.
//Instead of writing it like this
const obj = {one: one, two: two, three: three};
Solution 8:[8]
Arrow flavor:
const obj = (([one, two, three]) => ({one, two, three}))(arr)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | T.J. Crowder |
| Solution 2 | adambullmer |
| Solution 3 | Mario |
| Solution 4 | corse32 |
| Solution 5 | Technotronic |
| Solution 6 | Amit |
| Solution 7 | Islam Sayed |
| Solution 8 | Fabiano Taioli |
