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

mplungjan
  • 169,008
  • 28
  • 173
  • 236
mowgli
  • 2,796
  • 3
  • 31
  • 68
  • 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? – David Thomas Jul 03 '14 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. – cookie monster Jul 03 '14 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 – mowgli Jul 03 '14 at 20:00
  • I didn't know I had to simplify it to slice/concat.. all the solutions I found was very abstract/complicated.. Prototypes etc. – mowgli Jul 03 '14 at 20:01
  • And then I search "how to add removed item that was just removed back into array, at end point"? Why not just search for something that does both – mowgli Jul 03 '14 at 20:03
  • 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 , I 'm not really sure how that translates to this code here, can you do it for me?" – Jon Jul 03 '14 at 20:06
  • *"And then I search "how to add removed item that was just removed back into array, at end point"? "* Of course. That's a description of the minimal effort that should go into your research. – cookie monster Jul 03 '14 at 20:25
  • @cookiemonster Pff don't tell me about research and minimal effort, when I have truly spent a day searching about this issue. Now you're gonna comeback with "If that's the case then you would know by now that blabla" – mowgli Jul 03 '14 at 20:58
  • I prefer to believe that you're lying. If you actually spent a day researching and couldn't figure out this simple problem... well that's just sad. First Google search I did found the above duplicate. – cookie monster Jul 03 '14 at 21:00
  • Ah that link. Yes I tried that two times. And how does that function move the object to the unknown END? Why the need for a complex prototype function? How does it involve objects in a array? – mowgli Jul 03 '14 at 21:07
  • 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. – cookie monster Jul 03 '14 at 21:11

2 Answers2

16

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.

danguilherme
  • 642
  • 8
  • 23
  • Looks good and simple. Will try it. array of objects.. got it. thank you ;) – mowgli Jul 03 '14 at 20:15
  • 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 – mowgli Jul 03 '14 at 20:41
  • But to make it work I had to create a new array: var newarray = array.concat(itemToReplace); – mowgli Jul 03 '14 at 20:42
  • 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. – danguilherme Jul 03 '14 at 20:46
  • Yes, that works. And the DOM/references is ok with that? (I don't know anything about that) – mowgli Jul 03 '14 at 20:48
  • Yes, it is. Just be sure you're not assigning `itemToReplace` to a global variable. – danguilherme Jul 03 '14 at 21:01
  • Ok. Well, thank you. Nice and simple solution. As opposed to 20 lines of prototype mess – mowgli Jul 03 '14 at 21:09
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);
xio4
  • 216
  • 1
  • 4
  • 1
    `.push()`, not `.concat()` to mutate the original. – cookie monster Jul 03 '14 at 20:06
  • `.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 Jul 03 '14 at 20:17
  • 1
    @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);` – cookie monster Jul 03 '14 at 20:28
  • @cookiemonster: I got your point, you're right. – danguilherme Jul 03 '14 at 20:43