8

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")
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':...}} Commented Sep 10, 2012 at 19:32
  • This isn't really valid code - are you trying to build an object? Commented Sep 10, 2012 at 19:32
  • what does your code really look like? 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". 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. Commented Nov 21, 2019 at 19:32

6 Answers 6

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;
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry but what you describe is not an array but an simple JavaScript object. So it has no meaning to change the order as you can't iterate over it.
@AndreasKöberle There is no order here, they poster is swapping values.
@AndreasKöberle: The OPs question is somewhat vague and incomplete. I am presenting one option that may or may not help. He said nothing about iterating over it.
The question was about an array, one common behavior of an array is that you can iterate over it. When the OP ask vague question, whats the point to answer it before you can be sure what he really wants. I mean how can you give a correct answer to a vague question.
Yes, the only valid answers are those that are completely pigeon-holed and as narrow as possible. No other answer can possibly help. My apologies.
|
7

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/

Comments

2

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

Comments

1
arr = [0,1,2,3];
a = arr[3];
arr[3] = arr[0];
arr[0] = a;

Comments

0

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"

Comments

0

You can now do:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.