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);
Comments
Post a Comment