2014-03-28 59 views
-1

我有以下的JSON如何使用JavaScript或Jquery向上或向下移動數組項目?

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

我想數組項2移動到向上或向下。預期產出如下。

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

您可以通過交換功能做到這一點。 http://stackoverflow.com/questions/4011629/swapping-two-items-in-a-javascript-array – jadok

回答

3

您可以交換兩個元素,像這樣:

var x = EmployeeLists[2]; 
EmployeeLists[2] = EmployeeLists[1]; 
EmployeeLists[1] = x; 
相關問題