![](/img/trans.png)
[英]Updating next array in the array of objects with content from previous array
[英]assigning array previous objects to next element and updating the last
我需要我的應用程序需要將對象從上一個元素移到下一個元素,並更新最后一個。
我只是嘗試分配,但這可能是錯誤的方法,因為它不起作用-值是垃圾
let coordinate=[{x:4, y:3} ,{x:4, y:4},{x:4, y:5},{x:4, y:6}];
for(let i=0;i<coordinate.length-2;++i){
coordinate[i]=coordinate[i+1];
}
++coordinate[coordinate.length-1].y;
console.log(coordinate);
我認為這在邏輯上是正確的,但是也許我更新它的方式是錯誤的?
如果我做對了,您想要執行以下操作:刪除第一個元素,然后推送一個新元素,該元素是最后一個元素的更新版本。
let coordinate=[{x:4, y:3} ,{x:4, y:4},{x:4, y:5},{x:4, y:6}]; coordinate.shift(); const lastElm = coordinate[coordinate.length - 1]; coordinate.push({ ...lastElm, y: lastElm.y + 1 }); console.log(coordinate);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.