简体   繁体   English

JavaScript ES6-这是扩展语法还是其他语法?

[英]JavaScript ES6 - Is this spread syntax or rest syntax?

I would like to know as much as possible as to how this is working - especially as it relates to the usage of the ternary and the object argument containing two spreads. 我想尽可能地了解这是如何工作的-特别是因为它与包含两个扩展的三元和对象参数的用法有关。

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

First - the objects being passed into the map are structured like this: changed is shaped like this {"75864":{"ActType":"DEADLINE"}} 第一-对象被传递到地图被构造是这样的: changed的形状像这样{"75864":{"ActType":"DEADLINE"}}

rows is formatted like this (for example): 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                   "

This is "merging" row and changed[row.ID] into a single object. 这是“合并” rowchanged[row.ID]为单个对象。 Let's look at what happens when row is the one with the ID "75864": 让我们看看当row为ID为“ 75864”的row会发生什么:

// 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!

Note that object spread is essentially the same as using Object.assign with an empty object as the first argument. 请注意,对象散布本质上与使用Object.assign (第一个参数为空对象)相同。 (Object.assign takes all the properties from the second, third, etc arguments and sets them on the first argument. That means it actually changes - mutates - its first argument; and here, we aren't mutating row , we're returning a totally new object based on row (and changed[row.ID] ).) So writing your code with Object.assign would look like this: (Object.assign从第二个,第三个等参数中获取所有属性,并将它们设置在第一个参数上。这意味着它实际上会发生变化-mutates-其第一个参数;在这里,我们不是对row进行突变,而是返回一个基于 row全新对象(并changed[row.ID] 。)因此,使用Object.assign编写代码如下所示:

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

IF the row.ID value matches a key which has "truthy" value in the changed object, 如果row.ID值与在changed对象中具有“真实”值的键相匹配,

THEN you return a new object, with all values of the row (you spread the row ), and you add to this new object the values of the matching changed object (you spread the truthy value of changed ). 那么你回到一个新的对象,用的所有值row (你传播的row ),并且您添加到这个新对象匹配的值changed对象(你传播的truthy值changed )。

ELSE you simply return the given row. 否则,您只需返回给定的行。


Is it the answer your are looking for? 这是您要找的答案吗?

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

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