如何将关联数组转换为已排序的字符串数组?

时间:2015-05-25 14:39:23

标签: javascript arrays json

如何转换此js对象

 var obj1 =  {"user1":28, "user2":87, "user3":56};

进入一个字符串数组,按值排序,如下所示:

["user2","user3","user1"]

1 个答案:

答案 0 :(得分:10)

使用此:

var obj1 =  {"user1":28, "user2":87, "user3":56};
var a = Object.keys(obj1).sort(function(x,y){return obj1[y]-obj1[x]})
console.log(a);

输出:

["user2", "user3", "user1"]