简体   繁体   中英

Destructure array to object property keys

I have an array of values like:

const arr = [1,2,3];

You can assign destructurings 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.

I'm not sure if this helps, since you mentioned computed properties?!

I don't believe there's any structuring/destructuring solution to doing that in a single step, no. I wanted something similar in this question . The old := strawman proposal doesn't seem to have legs in the new proposal list , so I don't think there's much activity around this right now.

This answer shows a very neat two-step version.

But if it's two steps, you may as well use a simple object initializer:

 const arr = [1,2,3]; const obj = { one: arr[0], two: arr[1], three: arr[2] }; console.log(obj);

Another option is to do it with several temporary arrays but technically only one statement (I am not advocating this, just noting it):

 const arr = [1,2,3]; const obj = Object.fromEntries( ["one", "two", "three"].map((name, index) => [name, arr[index]] ) ); console.log(obj);

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 }

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

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 }),{})

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 }

 let distructingNames = ['alu', 'bob', 'alice', 'truce', 'truce', 'truce', 'truce', 'bob']; let obj={}; distructingNames.forEach((ele,i)=>{ obj[i]=ele; }) console.log('obj', obj)

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};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM