反转存储在 javascript 变量中的 JSON 数组

Posted

技术标签:

【中文标题】反转存储在 javascript 变量中的 JSON 数组【英文标题】:reverse the JSON Array stored in javascript variable 【发布时间】:2014-03-09 07:05:13 【问题描述】:

我有一个 json 数组以下列格式存储在变量中:

"info": 
[ "typeid": "877", "recid": "10", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "877", "recid": "11", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "459", "recid": "3", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "459", "recid": "4", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "456", "recid": "5", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "456", "recid": "6", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1"
]

我想反转内部 JSON 数组以获得 info.like this

"info": 
[ "typeid": "456", "recid": "6", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "456", "recid": "5", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "459", "recid": "4", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "459", "recid": "3", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "877", "recid": "11", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1",
  "typeid": "877", "recid": "10", "repeaterid": "0",  "pageid": "26966", "maxrecords": "1"
]

我怎样才能做到这一点。

【问题讨论】:

您是要对数组进行排序还是只反转它? 【参考方案1】:

使用javascript的数组reverse方法:

var objAssetSelection = $.parseJSON(strAssetSelection);
objAssetSelection.info.reverse();
console.log(objAssetSelection);

【讨论】:

获取未定义的信息。 您是否将var 替换为包含该对象的变量的实际名称? 啊,您还没有将 JSON 解析为对象。 我的错.....谢谢barmar。这很有帮助。最后一个问题是可以反转数组并根据 pageid 对它们进行排序吗?? 你可以对数组做任何你想做的事情。 Javascript 有一个sort 函数,ojovirtual 的回答显示了如何使用它。【参考方案2】:

你试过myObject.info.reverse()吗?

更多关于Javascript Array Reverse

【讨论】:

获取未定义的信息。【参考方案3】:

而且很简单(需要 JQuery):

function test() 
    var myArray = [1, 2, 3, 4];
    var myReversedArray = new Array();
    $(myArray).each(function (key) 
        myReversedArray.unshift(myArray[key]);
    );
    myArray = myReversedArray;
    $(myArray).each(function (key) 
        console.log(myArray[key]);
    );

【讨论】:

请注意,我的解决方案不是最快的,但我认为它是最简单的。我将我的与所有这些进行了比较:jsperf.com/js-array-reverse-vs-while-loop/20 结果是:53,390,542±1.33%85% 慢【参考方案4】:
var sorted=yourobject.info.sort(function(a,b)return a.typeid-b.typeid);

参考:http://www.w3schools.com/jsref/jsref_sort.asp

【讨论】:

【参考方案5】:

成功地将 JSON 数组转换为反向 JSON 数组 101% 工作

var ActualArray = [
        "bag":'large',
        "color":'blue'
    ,
        "bag":'small',
        "color":'red'
    ,
        "bag":'medium',
        "color":'green'
    ,
        "bag":'large',
        "color":'pink'
    ]
    var ReverseArray = [];
    var length = ActualArray.length;
    for(var i = length-1;i>=0;i--)
        ReverseArray.push(ActualArray[i]);
    
    console.log("actual array");
    console.log(JSON.stringify(ActualArray));
    console.log("reverse array");
    console.log(JSON.stringify(ReverseArray));

您的日志将如下所示

actual array
["bag":"large","color":"blue","bag":"small","color":"red","bag":"medium","color":"green","bag":"large","color":"pink"]
reverse array
["bag":"large","color":"pink","bag":"medium","color":"green","bag":"small","color":"red","bag":"large","color":"blue"]

【讨论】:

以上是关于反转存储在 javascript 变量中的 JSON 数组的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript中的变量在内存中的具体存储形式

在javascript中循环遍历python-dictionary-turned-into-json

javascript 在句子中反转单词并维持句子中的顺序

javascript的JSON对象

JavaScript中基本数据类型和引用数据类型的区别

Leetcode简单17. 整数反转JavaScript