Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 Array destructure and assign to object without functions

I was wondering if this preliminary code would be possible without a map , reduce or any other functions?

const arr1 = ['str', 'str2'];
let obj = {};
Object.assign(obj, [...arr1] : 'Value');
console.log(obj); // Expected output: { str1: 'Value', str2: 'Value' }

The whole point is to convert each array element into an object key and assign it a static value and what got my attention was whether it is possible by using a simple syntax and destructuring the array similarly to the pseudo code? Since I could not find any information on this I just want to get this question cleared out of my head.

like image 663
moodseller Avatar asked Nov 17 '25 16:11

moodseller


2 Answers

You can't spread the array elements to use them as property names for separate properties in an object like that.

The simplest way to do it would be just to use a loop after the fact:

let obj = {};
for (const str of arr1) {
    obj[str] = 'Value';
}

Live Example:

const arr1 = ['str', 'str2'];
let obj = {};
for (const str of arr1) {
    obj[str] = 'Value';
}
console.log(obj); // Expected output: { str1: 'Value', str2: 'Value' }

But if you want to go "fancier," you can use Object.assign and a bunch of temporary objects using spread notation and map and an arrow function:

let obj = {};
Object.assign(obj, ...arr1.map(str => ({[str]: 'Value'})));

See also Jared Smith's answer using fromEntries, which is very similar to this assign version but probably a bit more efficient (still creates a lot of unnecessary temporary objects, but processes those objects more efficiently).

Live Example:

const arr1 = ['str', 'str2'];
let obj = {};
Object.assign(obj, ...arr1.map(str => ({[str]: 'Value'})));
console.log(obj); // Expected output: { str1: 'Value', str2: 'Value' }

You can also shoe-horn it in to reduce if you want (because almost any array operation can be shoe-horned into reduce):

let obj = arr1.reduce((o, str) => {
    o[str] = 'Value';
    return o;
}, {});

Live Example:

const arr1 = ['str', 'str2'];
let obj = arr1.reduce((o, str) => {
    o[str] = 'Value';
    return o;
}, {});
console.log(obj); // Expected output: { str1: 'Value', str2: 'Value' }

And then perhaps (ab)using the comma operator so the arrow function can use a concise body:

let obj = arr1.reduce((o, str) => (o[str] = 'Value', o), {});

Live Example:

const arr1 = ['str', 'str2'];
let obj = arr1.reduce((o, str) => (o[str] = 'Value', o), {});
console.log(obj); // Expected output: { str1: 'Value', str2: 'Value' }

I'd keep it simple, though, and use the loop. :-)

like image 182
T.J. Crowder Avatar answered Nov 19 '25 07:11

T.J. Crowder


You can use the new Object.fromEntries if you wish:

var obj = Object.fromEntries(arr.map(str => [str, "Value"]));

Note that this is pretty new, and may need to be polyfilled.

like image 23
Jared Smith Avatar answered Nov 19 '25 09:11

Jared Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!