打字稿中[location.nlc,... groups]的目的是什么?

时间:2017-10-06 13:20:03

标签: typescript

我正在学习打字稿并且很新。目前,我正在尝试阅读一些项目代码,以便我能更快地理解 我对>>> sorted([sorted(i) for i in dummy_list_A]) [['A'], ['B'], ['C', 'D']] >>> sorted([sorted(i) for i in dummy_list_B]) [['A'], ['B'], ['C', 'D']] >>> sorted([sorted(i) for i in dummy_list_A]) == sorted([sorted(i) for i in dummy_list_B]) True 和const [location.nlc, ...groups]的使用感到困惑。说

nlc

以下代码将创建一个具有相同值并且键为“a”,“b”,“c”,“d”的字典。我的猜测是否正确?

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

1 个答案:

答案 0 :(得分:1)

这是一个带注释的示例,这里有一些简洁的TypeScript功能:

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