我试图找到一种方法将对象移动到数组的末尾
我有这个对象数组:
[{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}]
让我说我有一个身份证:3,或者一个职位:2。
有了这个,我想移动整套 {" id":" 3"," name":" Simon&#34 ;} 到最后的所有
我尝试了很多东西,搜索和搜索但是我无法使其工作
答案 0 :(得分:10)
var array = [{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}];
var itemToReplace = array.splice(0, 1); // 0 is the item index, 1 is the count of items you want to remove.
// => [{"id":"4","name":"Boaz"}]
array = array.concat(itemToReplace);
甚至更简单:
array = array.concat(array.splice(0, 1));
BTW:它是一个对象数组,而不是数组的对象。
答案 1 :(得分:5)
您可以使用splice和concat数组方法,例如
var arr = [{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}];
// Consider need move arr[2] to the end
var removed = arr.splice(2,1);
var new_arr = arr.concat(removed);