JavaScript ES6 - 这是扩展语法还是剩余语法?

标签 javascript ecmascript-6 spread-syntax

我想尽可能多地了解它是如何工作的——尤其是因为它与三元组的用法和包含两个展开的对象参数有关。

rows = rows.map(row => (changed[row.ID] ? { ...row, ...changed[row.ID] } : row));

首先 - 传递到 map 中的对象结构如下: changed 的形状像这样 {"75864":{"ActType":"DEADLINE"}}

rows 的格式如下(例如):

[{
    "ID": 75864,
    "NextDate": "2018-03-02T00:00:00",
    "NextTime": "1030am",
    "MatterID": 14116,
    "Descr": " Responses to pending discovery",
    "StatusID": 19,
    "Actor_s_": null,
    "Accrued": 0,
    "Go": "",
    "AspNetUserID": null,
    "DomainID": 2,
    "UserID": 1,
    "StatusType": "Pending",
    "ActTypeID": 50,
    "ActType": "DEADLINE",
    "MatterName": "WYNBAS                   "

最佳答案

这是将 rowchanged[row.ID]“合并”到一个对象中。让我们看看当 row 是 ID 为“75864”的行时会发生什么:

// row: {"ID": 75864, "ActType": "DEADLINE", (more properties)}
// changed: {"75864": {"ActType": "OTHER ACTION"}}
// (Note - I changed `changed` so that the ActType specified is different from
//  what's already in the row object, otherwise it's really difficult for me to
//  demonstrate exactly what's happening here.)

// This is the body of the arrow function:
return changed[row.ID] ? { ...row, ...changed[row.ID] } : row

// Well, changed[row.ID] does exist:
// changed[row.ID]: {"ActType": "OTHER ACTION"}

// So we take this branch of the ternary:
return { ...row, ...changed[row.ID] }

// Basically, this expression works like an array spread, but for objects.
// As a refresher, here's what an array spread might look like:
//
// a = [1, 2, 3]
// b = ['cat', 'dog', 'armadillo']
// c = [...a, ...b]
// c: [1, 2, 3, 'cat', 'dog', 'armadillo']
//
// The array spread works by creating a completely new, empty array. Then
// it adds the items of each array that's spread into it; so first it adds
// all the items of a (1, 2, 3), then all the items of b (cat, dog, armadillo).

// Object spread works pretty much the same way. First we create a completely
// new object: {}.
// Then we add all the properties of row: {ID: 75864, ActType: "DEADLINE",
// "MatterID": 14116, (more properties)}.
// Then it adds the the properties of changed[row.ID]. This is the important
// part, because changed[row.ID] actually *overwrites* any properties that
// we've already added from "row". This makes the result look like this:
return {ID: 75864, ActType: "OTHER ACTION", MatterID: 14116, (more properties)}

// Note that the return value's ActType property is OTHER ACTION, not DEADLINE!

请注意,对象传播与使用 Object.assign 基本相同以一个空对象作为第一个参数。 (Object.assign 从第二个、第三个等参数中获取所有属性,并将它们设置在第一个参数上。这意味着它实际上改变了 - 改变了 - 它的第一个参数;在这里,我们没有改变 row,我们将返回一个全新的对象基于 row(和changed[row.ID])。所以写你的代码与 Object.assign 看起来像这样:

return Object.assign({}, row, changed[row.ID])

关于JavaScript ES6 - 这是扩展语法还是剩余语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48907649/

相关文章:

javascript - 访问由 Object.create 创建的对象中的父( super )方法

javascript - 对于 in 和 Object.keys - es6

javascript - 如何使用扩展运算符向对象添加属性而不是覆盖它?

typescript - 使用解构/扩展在 TypeScript 中复制具有重命名属性的对象

java - 如何将 google api access_token 从java服务器传递到gapi javascript客户端?

javascript - 将 4 行 Jquery 转换为 Mootools

javascript - 过滤函数中的一行中有多个三元运算符

javascript - 使用 es6 将嵌套对象中的 props 合并为一个对象

javascript - 响应 native 上传图像的网络请求失败

javascript - 在较大的字符串中查找包含给定字母集的最小子字符串