Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个这样的对象:
{tsg2b: 1, fjdlf: 0}
现在我需要根据值对这个对象进行排序,例如我需要这样的结果:
{fjdlf: 0,tsg2b: 1}
您必须将对象放入数组中,然后对数组进行排序,因为无法在 java 脚本中对对象进行排序。尝试以下 -
var test = {tsg2b: 1, fjdlf: 0} var sortable = []; for (var item in test) sortable.push([item, test[item]]) sortable.sort(function(a, b) {return a[1] - b[1]})