簡體   English   中英

JavaScript ES6-這是擴展語法還是其他語法?

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

我想盡可能地了解這是如何工作的-特別是因為它與包含兩個擴展的三元和對象參數的用法有關。

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

第一-對象被傳遞到地圖被構造是這樣的: 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會發生什么:

// 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從第二個,第三個等參數中獲取所有屬性,並將它們設置在第一個參數上。這意味着它實際上會發生變化-mutates-其第一個參數;在這里,我們不是對row進行突變,而是返回一個基於 row全新對象(並changed[row.ID] 。)因此,使用Object.assign編寫代碼如下所示:

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

如果row.ID值與在changed對象中具有“真實”值的鍵相匹配,

那么你回到一個新的對象,用的所有值row (你傳播的row ),並且您添加到這個新對象匹配的值changed對象(你傳播的truthy值changed )。

否則,您只需返回給定的行。


這是您要找的答案嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM