更改從數(shù)組的元素賦值的變量時(shí),數(shù)組中的特定元素將隨變量而變化。但是,代碼中沒(méi)有要更改的數(shù)組。在代碼中,數(shù)組是一個(gè)對(duì)象數(shù)組,變量在函數(shù) randomCentroid(質(zhì)心)內(nèi)聲明和賦值,該函數(shù)在初始化質(zhì)心中調(diào)用。該代碼用于生成有關(guān) k 均值聚類(lèi)的隨機(jī)質(zhì)心,因此代碼://Return random centroid points with coloringlet 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 pointslet 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: “質(zhì)心-2”}1: {}2: {填充: “#ff7f0e”, id: “質(zhì)心-1”}3: {填充: “#1f77b4”, id: “質(zhì)心-0”}4: {}長(zhǎng)度: 5原型: 數(shù)組(0)如何刪除由數(shù)組分配的對(duì)象組成的變量與數(shù)組之間的依賴(lài)關(guān)系?
數(shù)組與由該數(shù)組中的元素分配的變量之間的奇怪依賴(lài)關(guān)系
慕絲7291255
2022-09-16 21:12:41