typescript中[location.nlc,…groups]的用途是什么?
我正在学习打字脚本,而且对它非常陌生。目前,我正在尝试阅读一些项目代码,以便能够更快地理解。typescript中[location.nlc,…groups]的用途是什么?,typescript,Typescript,我正在学习打字脚本,而且对它非常陌生。目前,我正在尝试阅读一些项目代码,以便能够更快地理解。 我对[location.nlc,…groups]和constnlc的用法感到困惑。说 groups=['b','c','d']; location.nlc=“a” 下面的代码将创建一个具有相同值和键“a”、“b”、“c”、“d”的字典。我的猜测正确吗 const groups = location.groups ? location.groups.split(",") : []; const c
我对
[location.nlc,…groups]
和constnlc
的用法感到困惑。说
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;
}
这是一个带注释的示例,这里有一些整洁的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]
或许可以阅读新的es6运营商和功能?