admin管理员组

文章数量:1193760

I have nested array in javascript like this:

testArray['element1'] = {par1: "value1", par2: "value2" ... }
testArray['element2'] = {par1: "value1", par2: "value2" ... }
testArray['element3'] = {par1: "value1", par2: "value2" ... }
testArray['element4'] = {par1: "value1", par2: "value2" ... }

so how can I change place of element? for example instead of

("element1", "element2", "element3","element4")
to be
("element4", "element2", "element3","element1")
or
("element1", "element4", "element3","element2")

I have nested array in javascript like this:

testArray['element1'] = {par1: "value1", par2: "value2" ... }
testArray['element2'] = {par1: "value1", par2: "value2" ... }
testArray['element3'] = {par1: "value1", par2: "value2" ... }
testArray['element4'] = {par1: "value1", par2: "value2" ... }

so how can I change place of element? for example instead of

("element1", "element2", "element3","element4")
to be
("element4", "element2", "element3","element1")
or
("element1", "element4", "element3","element2")
Share Improve this question edited Sep 10, 2012 at 19:47 Irakli asked Sep 10, 2012 at 19:29 IrakliIrakli 1,1436 gold badges30 silver badges56 bronze badges 5
  • 1 'element1' = { is not valid syntax. Do you have an array of objects? in which case it would be: array = [ {'element1':{'par1':...}}, {'element2':{'par1':...}}] Or is your array actually an associative array (aka js object). In that case it would be: array = {'element1':{'par1'..}, 'element2':{'par1':...}} – driangle Commented Sep 10, 2012 at 19:32
  • This isn't really valid code - are you trying to build an object? – Stephen Commented Sep 10, 2012 at 19:32
  • what does your code really look like? – epascarello Commented Sep 10, 2012 at 19:46
  • 3 What you describe is not an array but an JavaScript object in the JSON notation. So as this is no array there will be no order of the elements in it and you cant "change the place". – Andreas Köberle Commented Sep 10, 2012 at 19:57
  • I think this question should be deleted because it doesn't correctly describe it's goal and it doesn't use correct terminology for the language. It is a misleading title and a poorly described question. Either that, or it needs to be edited quite a bit. – ADJenks Commented Nov 21, 2019 at 19:32
Add a comment  | 

6 Answers 6

Reset to default 7

First, build the object properly:

array = {
  'element1' : {par1: 'value1', par2: 'value2', par3: 'value3'....},
  'element2' : {par1: 'value1', par2: 'value2', par3: 'value3'....},
  'element3' : {par1: 'value1', par2: 'value2', par3: 'value3'....},
  'element4' : {par1: 'value1', par2: 'value2', par3: 'value3'....}
}

Then swap:

var tmp = array['element2'];
array['element2'] = array['element1'];
array['element1'] = tmp;

What you've posted in your question is not an array, it's not even valid javascript syntax. Since you ask about order, I'll assume you are not using objects as objects in javascript have no guaranteed order.

That being said, I'm going to assume you have an array declared as such:

var testArray = [{ ... }, { ... }, { ... }];

To swap two elements, you just need a generic swap function:

var swap = function(theArray, indexA, indexB) {
    var temp = theArray[indexA];
    theArray[indexA] = theArray[indexB];
    theArray[indexB] = temp;
};

swap(testArray, 0, 1);

http://jsfiddle.net/jbabey/gRVn5/

you can add it as an array prototype like so:

Array.prototype.swap = function (index1, index2) {
    if (index1 <= this.length && index2 <= this.length) {
        var temp = this[index2];
        this[index2] = this[index1];
        this[index1] = temp;
    }
};
arr = [0,1,2,3];
a = arr[3];
arr[3] = arr[0];
arr[0] = a;

I'd just write a swap function.

var letters = "abcdefghijklmnopqrstuvwxyz".split("");

function swap(theArray, index1, index2) {
  var temp = theArray[index2];
  theArray[index2] = theArray[index1];
  theArray[index1] = temp;
}

swap(letters, 2, 25); // swap "c" and "z"

You can now do:

let list = [1,2,3,4];
[list[1],list[3]] = [list[3],list[1]];

//Result: [1,4,3,2]

本文标签: Javascript change place of array elementStack Overflow