Problem
Say we have an array of values:
const values = [ 10, 20, 30 ];
Say we have an object that we want to pick up these values:
let props = {
"hello": null,
"world": null,
"other": null
};
I could simply do this:
props.hello = values[0];
props.world = values[1];
props.other = values[2];
That way we'd end up with the desired result:
console.log(props.hello); // Result: 10
console.log(props.world); // Result: 20
console.log(props.other); // Result: 30
Or, more accurately:
console.log(props);
// Result:
// {
// "hello": 10,
// "world": 20,
// "other": 30
// }
The problem is I don't always know what the names of the properties will need to be.
Suppose props is an existing but empty object (or if it does contain properties, they are unrelated to what we're doing here).
props = {};
And suppose we want to compute the name of each property from some related value.
How can I populate props with the names and values I want?
One solution
So I could do this instead, reducing an array of names to an object and then assigning that object's properties back to the original object:
const names = [ "hello", "world", "other" ];
const newProps = names.reduce((p, c, i) => {
p[c] = values[i];
return p;
}, {});
props = Object.assign({}, props, newProps);
But this seems messy. For one, it assumes that for every i that exists in values the equivalent would exist for names.
Question
Is there a way to do this with a destructuring assignment?
Yes, there is if I know the names of the properties beforehand:
[ props.hello, props.world, props.other ] = values;
But what if the names exist separately or need to be calculated before they can be assigned to the props object?
let props = {};
const values = [ 10, 20, 30 ];
const names = [ "hello", "world", "other" ];
// [ ...props??? ] = values;
//
// Desired result (props):
// {
// "hello": 10,
// "world": 20,
// "other": 30
// }
One other thing: The reason I'm interested in destructuring, apart from the conciseness of the form, is that I'd like names and values to be considered "immutable" once declared. The object props would ideally be the only changing state in the code. (I've hinted at this by declaring names and values with const, and props with let, although I know in practice these declarations don`t stop the contents of objects and arrays from being changed.)
const props = Object.fromEntries(names.map((name, i) => [name, values[i]]))Not using destructuring, but would work. I don't think destructuring alone could do thisvalues[i]might not exist. With that precondition, how would you expect any form of array destructuring to work in the first place?inecessarily exists innames, it would also exist invalues. Destructuring allows for default values where the property doesn't exist but also has a rest operator for properties that do but might not otherwise be captured. It seems like a neater way to handle this... if it's even possible.