I've got two arrays of strings of equal length, and I want to make a key:pair object out of them, like:
INPUT:
let words1 = ["foo", "loo", "who"];
let words2 = ["bar", "car", "mar"];
DESIRED OUTPUT:
{
foo: "bar",
loo: "car",
who: "mar"
}
I originally accomplished this by doing:
const wordMap = {};
words1.map((word,n) => {
wordMap[word] = words2[n];
})
However, our linter is throwing an error to "prefer destructuring". I can't seem to figure out how to do this using destructuring - and given that I'm pretty new to destructuring, I figure I must just be missing something. I'm struggling to find a clean way to do this... I figure the answer might resemble something like:
{[words1]:[...words2]};
Can't figure it out though. Here's a JSFiddle I was toying around with too. Let me know if there's a way to do this. Thanks!
I can't seem to figure out how to do this using destructuring
The linter wants you to write
({[n]: wordMap[word]} = words2);
instead of
wordMap[word] = words2[n];
This is complete bullocks - less readable, more convoluted, and not even shorter than the simple and straightforward assignment. Ignore the rule here, it doesn't make sense - or disable it altogether, maybe at least the enforceForRenamedProperties
option or the AssignmentExpression
option.