更改从数组的元素赋值的变量时,数组中的特定元素将随变量而变化。但是,代码中没有要更改的数组。
在代码中,数组是一个对象数组,变量在函数 randomCentroid(质心)内声明和赋值,该函数在初始化质心中调用。
该代码用于生成有关 k 均值聚类的随机质心,因此代码:
//Return random centroid points with coloring
let randomCentroid = (fill) => {
let r = Math.floor(Math.random()*points.length);
let centroid = points[r];
//points.splice(r, 1);
centroid.fill = fill;
return centroid;
}
//Gives distinct coloring to all centroid points
let initializeCentroids = (num) => {
let allCentroids = [];
for (let i = 0; i < num; i++) {
let color = colors(i);
let centroid = randomCentroid(color);
centroid.id = 'centroid' + '-' + i;
allCentroids.push(centroid);
}
return allCentroids;
}
//same for const.
let points = [{}, {}, {}, {}, {}],
colors = d3.scaleOrdinal(d3.schemeCategory10);
initializeCentroids(3);
console.log(points);
安慰:
索引.js:29
(5) [{...}, {...}, {...}, {...}, {...}]
0: {填充: “#2ca02c”, id: “质心-2”}
1: {}
2: {填充: “#ff7f0e”, id: “质心-1”}
3: {填充: “#1f77b4”, id: “质心-0”}
4: {}
长度: 5
原型: 数组(0)
如何删除由数组分配的对象组成的变量与数组之间的依赖关系?
料青山看我应如是
相关分类