交换JSON数组中的键值

时间:2014-03-10 08:23:52

标签: javascript

我有以下JSON。我需要交换SortId

就像我有这个,

[{"CategoryId":1,"Name":"Worktable","SortId":1}
,{"CategoryId":2,"Name":"Bf ","SortId":2}]

交换'SortId'之后我需要

[{"CategoryId":1,"Name":"Worktable","SortId":2}
,{"CategoryId":2,"Name":"Bf ","SortId":1}]

请告诉我如何通过JavaScript来实现。

1 个答案:

答案 0 :(得分:1)

var tmp = a[0].SortId;
a[0].SortId = a[1].SortId;
a[1].SortId = tmp;

jsFiddle