8

I have a json array stored in variable in format below:

{"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"}
]}

I want to reverse the inner JSON array for 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"}
]}

How can i achieve this.

enter image description here

1
  • 1
    Would you like to sort the array or just reverse it? Commented Feb 11, 2014 at 13:13

5 Answers 5

21

Use the array reverse method of Javascript:

var objAssetSelection = $.parseJSON(strAssetSelection);
objAssetSelection.info.reverse();
console.log(objAssetSelection);
Sign up to request clarification or add additional context in comments.

7 Comments

getting undefined for info.
Did you replace var with the actual name of your variable that contains the object?
Ahh, you haven't parsed the JSON into an object yet.
My bad.....Thanks barmar. That was very helpful. one last question is it possible to reverse the array as well as sort them on the basis of pageid??
You can do whatever you want with the array. Javascript has a sort function, ojovirtual's answer shows how to use it.
|
6

Did you tried myObject.info.reverse() ?

More about Javascript Array Reverse

1 Comment

getting undefined for info.
2
var sorted=yourobject.info.sort(function(a,b){return a.typeid-b.typeid});

Ref: http://www.w3schools.com/jsref/jsref_sort.asp

Comments

2

and simply (JQuery needed) :

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

1 Comment

Note my solution is not the quickest but I think it's the simpliest. I compared mine to all of those : jsperf.com/js-array-reverse-vs-while-loop/20 Result was : 53,390,542±1.33%85% slower
0

Successfully Working Converting JSON Array to Reverse JSON Array 101% Working

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

Your Log will be like below

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"}]

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.