-1

I have the following json

{  "EmployeeLists": [
{
  "ID": 1,
  "NAME": "Anand",
  "Salary": 90000
},
{
  "ID": 2,
  "NAME": "Anbu",
  "Salary": 80000 
},
{
  "ID": 3,
  "NAME": "Bala",
  "Salary": 85000
}  ]}

I want to move the array item 2 to up or down. the expected output is like below.

{  "EmployeeLists": [
{
  "ID": 2,
  "NAME": "Anbu",
  "Salary": 80000
},
{
  "ID": 1,
  "NAME": "Anand",
  "Salary": 90000
},
{
  "ID": 3,
  "NAME": "Bala",
  "Salary": 85000
} ]}
CHANDRA
  • 4,778
  • 8
  • 32
  • 51
  • You can do it by the swap function. http://stackoverflow.com/questions/4011629/swapping-two-items-in-a-javascript-array – jadok Mar 28 '14 at 13:18

1 Answers1

4

You can swap the 2 elements like so:

var x = EmployeeLists[2];
EmployeeLists[2] = EmployeeLists[1];
EmployeeLists[1] = x;
Marius Stănescu
  • 3,603
  • 2
  • 35
  • 49