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

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -