Javascript 如何将数组的最后一个元素重新排列到第一位?

Javascript 如何将数组的最后一个元素重新排列到第一位?,javascript,Javascript,我有一个javascript对象数组: shapeToolsTarget: Array(4) 0: {id: 20, name: "Background", type: "shape"} 1: {id: 21, name: "BorderColor", type: "shape"} 2: {id: 22, name: "BorderWeight", type: "shape"} 3: {id: 3, name: "Paste", type: "text"} 如何将数组的最后一个元素重新排列到第

我有一个javascript对象数组:

shapeToolsTarget: Array(4)
0: {id: 20, name: "Background", type: "shape"}
1: {id: 21, name: "BorderColor", type: "shape"}
2: {id: 22, name: "BorderWeight", type: "shape"}
3: {id: 3, name: "Paste", type: "text"}
如何将数组的最后一个元素重新排列到第一位?就像这样:

shapeToolsTarget: Array(4)
0: {id: 3, name: "Paste", type: "text"}
1: {id: 20, name: "Background", type: "shape"}
2: {id: 21, name: "BorderColor", type: "shape"}
3: {id: 22, name: "BorderWeight", type: "shape"}

您可以使用和的组合

unshift方法将一个或多个元素添加到数组的开头,并返回数组的新长度

pop方法从数组中删除最后一个元素并返回该元素。此方法更改数组的长度

var项目=['A','B','C']; console.logitems; items.unshiftitems.pop; console.logitems 您可以对以下内容使用数组方法(&A):

pop从数组中删除最后一个元素。 unshiftvalue将值添加到数组的开头。 让shapeToolsTarget=[ {id:20,name:Background,type:shape}, {id:21,名称:BorderColor,类型:shape}, {id:22,name:BorderWeight,type:shape}, {id:3,名称:粘贴,类型:text}]; shapeToolsTarget.unshiftshapeToolsTarget.pop
console.LogShapeToolStarGet您是否尝试了一些可以展示给我们的内容?那我们就可以help@Calvin努内斯:我试过数组移位函数,但没有结果。谢谢!但是我有4个元素而不是3个,我得到了0:{id:22,name:BorderWeightComponent,type:shape}1:{id:3,name:Paste,type:text}2:{id:20,name:Background,type:shape}3:{id:21,name:Border,type:shape}数组中元素的数量和类型并不重要,我添加了一个描述来说明这两种方法是如何工作的。只需执行shapeToolsTarget.unshiftshapeToolsTarget.pop;。可以但我把这个元素排在了第二位!而不是先…谢谢你!但是我用你的代码0:{id:22,name:BorderWeightComponent,type:shape}1:{id:3,name:Paste,type:text}2:{id:20,name:Background,type:shape}3:{id:21,name:Border,type:shape}它在第二位..代码更新了。请再试一次。