2017-10-06 66 views
0

我正在學習打字稿,而且很新。目前,我正在嘗試閱讀一些項目代碼,以便更快地理解它。
我對使用[location.nlc, ...groups]和const nlc感到困惑。說打字稿中[location.nlc,... groups]的用途是什麼?

groups=['b','c','d']; location.nlc="a";

下面的代碼只是創建相同的價值和關鍵 'A', 'B', 'C', 'd' 的字典。我的猜測是正確的嗎?

const groups = location.groups ? location.groups.split(",") : []; 
    const clusters: ClusterMap = {}; 
    for (const nlc of [location.nlc, ...groups]) { 
     clusters[nlc] = nlc; 
    } 
+0

可能會閱讀新的es6操作符和功能? – toskv

+0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator –

回答

1

下面是一個註釋例如,有一些巧妙的打字稿功能在這裏:

// if the loc.groups has a value, split it by comma (otherwise use an empty array) 
const groups = loc.groups ? loc.groups.split(",") : []; 

// variable for the cluster map 
const clusters: ClusterMap = {}; 

// for each string (nlc) in the expanded array of loc.nlc (which is 'z'), and all the items in groups (which are a, b, c, d) 
for (const nlc of [loc.nlc, ...groups]) { 
    // add the item to the cluster map with a key of (for example 'z') 
    // and a value of (for exmaple 'z') 
    clusters[nlc] = nlc; 
} 

最終結果是:

{ 
    z: 'z', 
    a: 'a', 
    b: 'b', 
    c: 'c', 
    d: 'd' 
} 

在本例中最酷的功能:

const arr1 = [1, 2, 3]; 
const arr2 = [4, 5, 6]; 

// 0,1,2,3,4,5,6 
const combined = [0, ...arr1, ...arr2]