Move object in array to end

arrays, javascript, jquery, object

Solution

You can `splice` and then `concat` the object you want to remove:

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);

or even simpler:

array = array.concat(array.splice(0, 1));

BTW: it's an array of objects, not an object of arrays.

Problem

I'm trying to find a way to move an object to the end of the array I have this array of objects: ``` [{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}] ``` Let's say I have an id: 3, or a position: 2. With that I want to move the whole set {"id":"3","name":"Simon"} to the end of it all I have tried so many things, and searched and searched but I can't make it work

Original source

Related problems