admin管理员组文章数量:1435859
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.
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.
Share Improve this question asked Dec 4, 2018 at 18:40 moodsellermoodseller 2124 silver badges14 bronze badges3 Answers
Reset to default 6You 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 ma 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. :-)
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.
It is not possible to spread a value and get an object with this item as key and another value.
You need to get an object with the wanted interior by adding a custom generator to the array which retunrs an object for each item.
const arr1 = ['str', 'str2'];
arr1[Symbol.iterator] = function* () {
for (let i = 0; i < this.length; i++) yield { [this[i]]: 'Value' };
};
let obj = {};
Object.assign(obj, ...arr1);
console.log(obj);
本文标签: javascriptES6 Array destructure and assign to object without functionsStack Overflow
版权声明:本文标题:javascript - ES6 Array destructure and assign to object without functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745654718a2668640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论