3

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

11
  • 3
    Saying that you've "searched and searched" implies you've undertaken research, but failing to show (any of) your attempts doesn't help. What have you tried (even in pseudo-code), and where did it go wrong? Commented Jul 3, 2014 at 19:55
  • What specifically is the problem? Do you not know how to remove an item from an Array? Or how to add an item to the end? Those are two very basic tasks. I'd find it hard to believe that you couldn't find any information anywhere that showed you how to do those. Commented Jul 3, 2014 at 19:59
  • @Jon Come on, we can't all be experts.. I'm a noob with objects/arrays ok?. Or is that not ok? I have searched "jquery move array object" and "javascript move array object" and things like that Commented Jul 3, 2014 at 20:00
  • 2
    @mowgli: Of course it's OK to be a newbie. But the difference between object of arrays and array of objects is "kind of important". So I would expect a well-meaning newbie to get that nailed down first and then go asking the next question. Not "hey, I want to do <insert description>, I 'm not really sure how that translates to this code here, can you do it for me?" Commented Jul 3, 2014 at 20:06
  • 1
    And this is what I mean. If you couldn't figure it out at first, that Q&A should at least get you 95% of the way there. Some of the answers put a new function on the .prototype, others do not. The only way objects in an array would make a difference would be if you don't know how to get the property from an object. Simple problem solving skills is all it takes for a beginner to do this. Commented Jul 3, 2014 at 21:11

2 Answers 2

17

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Looks good and simple. Will try it. array of objects.. got it. thank you ;)
Got it working. Turns out, I needed to change order in a slider I'm using too.. strangely I had to put default_id = -1 at first and increment BEFORE the fades (not use 0 even though the first array position is zero hm). But now it's all good, phew. Thanks
But to make it work I had to create a new array: var newarray = array.concat(itemToReplace);
You don't need to create a new array, you can assign the concatenated array to its old var: array = array.concat(itemToReplace);. I will edit my answer to cover this.
Yes, that works. And the DOM/references is ok with that? (I don't know anything about that)
|
5

You can use splice and concat array methods like

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

4 Comments

.push(), not .concat() to mutate the original.
.push() will add an array of the removed items, not the items themselves. In this case it's an array with one item only, but the .concat() is still the best choice, instead of doing .push(removed[0]) and taking the chance of adding an "undefined" string in the original array.
@Danguilherme: .concat doesn't mutate the original, so now there are two different Arrays. Even if he overwrites the arr variable, there still could be other references to the original that reference stale data. That's why .push() should be used. Naturally the correct element needs to be extracted, or just arr.push.apply(arr, removed);
@cookiemonster: I got your point, you're right.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.