这个 Object.assign({}, ...array.map(/*...*/)) 代码在这里做什么

发布于 2025-01-12 08:32:59 字数 186 浏览 2 评论 0原文

我已经从 github 克隆了这个存储库,但我无法理解代码中发生的情况。

代码中的words是一个单词数组。

const wordDictionary = Object.assign({}, ...words.map((x) => ({ [x]: x })));

I have cloned this repo from github and I am unable to understand what's happening in the code.

words in the code is an array of words.

const wordDictionary = Object.assign({}, ...words.map((x) => ({ [x]: x })));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2025-01-19 08:32:59

它采用一个数组(字符串数组,因为您说它是单词数组;但它也可以使用数字或符号)并将其转换为属性名称和值都相同的对象。例如,["an", "example"] 变为 {an: "an", example: "example"}

const words = ["an", "example"];
const wordDictionary = Object.assign({}, ...words.map((x) => ({ [x]: x })));
console.log(wordDictionary);

它通过将每个数组元素映射到具有一个属性的对象(由数组元素命名,并带有数组元素的值),然后将所有这些对象分配给单个目标对象来实现这一点。

例如,对于我的 ["an", "example"] 数组,步骤如下:

  1. 进行映射、转动:
    <前><代码>[“一个”,“示例”]

    进入

    [{an: "an"}, {example: "example"}]
    
  2. 将该数组扩展为离散参数Object.assign 带有空白目标对象。所以
    Object.assign({}, ...[{an: "an"}, {example: "example"}])
    

    变成

    Object.assign({}, {an: "an"}, {example: "example"})
    
  3. 使用 Object.assign 将这些对象的属性分配给第一个对象(空白 {} 对象)。

值得注意的是,代码过于复杂。使用循环会更简单:

const wordDictionary = {};
for (const word of words) {
    wordDictionary[word] = word;
}

单独地,这样的字典可以通过 Map 实例:

const wordDictionary = new Map();
for (const word of words) {
    wordDictionary.set(word, word);
}

或者在这种特定情况下(因为键和值相同)设置

const wordDictionary = new Set(words);

It's taking an array (of strings, since you said it's an array of words; but it would work with numbers or Symbols too) and converting it to an object where both the names and values of the properties are the same. So for example, ["an", "example"] becomes {an: "an", example: "example"}:

const words = ["an", "example"];
const wordDictionary = Object.assign({}, ...words.map((x) => ({ [x]: x })));
console.log(wordDictionary);

It does it by mapping each array element to an object with one property (named by the array element, with the value of the array element) and then assigning all of those objects to a single target object.

For instance, with my ["an", "example"] array, the steps are:

  1. Do the mapping, turning:
    ["an", "example"]
    

    into

    [{an: "an"}, {example: "example"}]
    
  2. Spread that array out into discrete arguments to Object.assign with a blank target object. So
    Object.assign({}, ...[{an: "an"}, {example: "example"}])
    

    becomes

    Object.assign({}, {an: "an"}, {example: "example"})
    
  3. Use Object.assign to assign the properties from those objects to the first one (the blank {} object).

It's worth noting that the code is overcomplicated. It would be much simpler just to use a loop:

const wordDictionary = {};
for (const word of words) {
    wordDictionary[word] = word;
}

Separately, dictionaries like this are better handled by Map instances:

const wordDictionary = new Map();
for (const word of words) {
    wordDictionary.set(word, word);
}

or in this specific case (since the key and value are the same) a Set:

const wordDictionary = new Set(words);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文