2

Possible Duplicate:
JavaScript Array rotate()

I have a tricky question for you. I have an object who contain different values and I want to change the index of on element so My first element has to become the last or the third or whatever I want. Is that possible in javascript ??

var object = [{0},{1},{2}]

to

var object = [{2},{0},{1}]

This is an example of what I would like to do.

Thanks in advance.

Community
  • 1
  • 1
Simon
  • 1,153
  • 1
  • 16
  • 36
  • You can't, as this isn't valid JavaScript. And BTW: you're `object` var is an array, that contains malformed object literals. Also: don't use possible reserved words as variable names – Elias Van Ootegem Sep 06 '12 at 10:57
  • @EliasVanOotegem clearly this is example code – thomaux Sep 06 '12 at 10:58
  • @Anzeo: Even so, it makes it too difficult to answer: the way you could sort the array depends on how its containing objects are structured, or is he looking for a way to sort the keys of _any_ type of object? – Elias Van Ootegem Sep 06 '12 at 11:00
  • @EliasVanOotegem what does sorting have to do with that?.. – Qnan Sep 06 '12 at 11:01
  • @Qnan read the question again – thomaux Sep 06 '12 at 11:01
  • @Anzeo i have. It says "move an object inside an array". Doesn't say sort anywhere – Qnan Sep 06 '12 at 11:03
  • @Qnan "...My first element has to become the last or the third or whatever I want..." – thomaux Sep 06 '12 at 11:04
  • @Anzeo so? where does it say sort? – Qnan Sep 06 '12 at 11:04
  • @Qnan: What do you call it when you're reorganizing a group of things? making a mess? or do you tend to _sort_ according to some logical pattern? It needn't be alphabetically or numerically, but in my book: you're sorting – Elias Van Ootegem Sep 06 '12 at 12:17
  • @EliasVanOotegem mess is not a very technical term. But Knuth Shuffle (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) involves this kind of operation. Do you call that sorting? – Qnan Sep 06 '12 at 12:33
  • @Qnan, Yes. In fact, from that very page: _A variant of the above method that has seen some use in languages that support sorting with user-specified comparison functions is to shuffle a list by sorting it with a comparison function that returns random values_. As luck would have it: JS is such a language: `Array.prototype.sort = function(){}`. But this really is a pointless discussion, isn't it? I do like the link you gave me, it's a good read, thanks for that – Elias Van Ootegem Sep 06 '12 at 12:49

2 Answers2

3

Your from and to example is a little hard to understand the intent, but based on the body of your question asking

My first element has to become the last or the third or whatever I want

Writing a method to swap 2 elements in an array is easy enough:

function swapElements(a, x, y){
    var temp = a[x];
    a[x] = a[y];
    a[y] = temp;
}

Live example: http://jsfiddle.net/pwY9L/

Jamiec
  • 118,012
  • 12
  • 125
  • 175
1

I assume you mean:

var array = [0,1,2];

It doesn’t really matter if the array contains numbers, objects or whatever. If you want to modify this array, there are many ways to do this in javascript andyou can read up about all the Array prototypes here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array

F.ex, move the last one and place it first:

array.unshift(array.pop());
David Hellsing
  • 97,234
  • 40
  • 163
  • 203