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.
Use the array reverse
method of Javascript:
var objAssetSelection = $.parseJSON(strAssetSelection);
objAssetSelection.info.reverse();
console.log(objAssetSelection);
Did you tried myObject.info.reverse()
?
More about Javascript Array Reverse
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]);
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With