【发布时间】:2020-02-13 11:56:44
【问题描述】:
我有一个包含变量的数组。我想将一些 x 和 y 坐标推送到数组中,但是当我尝试推送而不是更改变量值时,推送会创建新变量并将值分配给这些变量。
原来是这样的
0: {id: "t523470", name: "tPm1", x: 0, y: 0, parent: null, …}
1: {id: "t523471", name: "tPm2", x: 0, y: 0, parent: null, …}
2: {y: 651, x: 446}
3: {y: 800.015625, x: 802.328125}
不是这个
0: {id: "t523470", name: "tPm1", x: 446, y: 651, parent: null, …}
1: {id: "t523471", name: "tPm2", x: 802.328125, y: 800.015625, parent: null, …}
function getModulePosition(id) {
for (let i = 0; i < axisViewElements.modules.length; i++) {
let currentElement = axisViewElements.modules[i].id;
if (id === currentElement) {
continue;
}
let module = document.getElementById(currentElement);
let modulePos = { x: module.getBoundingClientRect().left, y: module.getBoundingClientRect().top }
console.log(modulePos);
axisViewElements.modules.push({
y: modulePos.y,
x: modulePos.x
});
}
}
【问题讨论】:
标签: javascript arrays sorting d3.js svg