javascript - Sorting key value pair on basis of another array -


i have key value pairs :

var x={1:car, 2: cycle, 3:john } 

this response coming json.[object object] have array :var arr=[1,3,2] want sort x per arr . order should : {1:car,3:john,2:cycle}

in javascript how achieve this.

you don't need sort them, make new empty array , populate getting values of arr , using them index of x.

var x = {   1: 'car',   2: 'cycle',   3: 'john' }; var arr = [1, 3, 2];  var output = []; arr.foreach(function(item){     output.push(x[item]); }); console.log(output); 

fiddle.


Comments

Popular posts from this blog

PHP while loop dynamic rowspan -

timer - Java TimerTask cancel -

android - How to read a column value in parse.com without accessing an object or a pointer -