简体   繁体   English

解构 JavaScript 数组以计算现有对象的属性名称

[英]Destructure JavaScript array to computed property names of existing object

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是一个存在但空的对象(或者如果它确实包含属性,它们与我们在这里所做的无关)。

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?如何用我想要的名称和值填充props

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 .一方面,它假设对于存在于values的每个i ,则存在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?但是如果名称单独存在或者需要计算才能分配给props对象呢?

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.另一件事:除了形式的简洁性之外,我对解构感兴趣的原因是我希望namesvalues一旦声明就被认为是“不可变的”。 The object props would ideally be the only changing state in the code.理想情况下,对象props应该是代码中唯一变化的状态。 (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声明namesvalues并使用let声明props来暗示这一点,尽管我知道实际上这些声明不会阻止对象和数组的内容被更改。)

You could iterate the names and values at the same time for assignment.您可以同时迭代名称和值以进行分配。

 let values = [10, 20, 30], props = {}, names = ["hello", "world", "other"]; for (props[names.shift()] of values) ; console.log(props);

Another thinkable solution, but with hardcoded object reference.另一个可行的解决方案,但使用硬编码的对象引用。

 let values = [10, 20, 30], props = {}, names = ["hello", "world", "other"]; [props[names.shift()], props[names.shift()], props[names.shift()]] = values; console.log(props);

The expected result could be obtained as follows:可以得到预期的结果如下:

 let values = [ 10, 20, 30 ]; let names = [ "hello", "world", "other" ]; let props = {}; names.forEach((name, index) => props[name] = values[index]); console.log(props);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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