将对象文字转换为已排序的数组

时间:2021-02-12 21:21:10

I have an object literal, where the values of its key are more objects, and one of the keys of the inner objects is named "rank" - and has an floating point value. I want to convert the object literal to an array of the inner objects, sorted by the value of "rank".

我有一个对象文字,其键的值是更多的对象,内部对象的一个​​键被命名为“rank” - 并具有浮点值。我想将对象文字转换为内部对象的数组,按“rank”的值排序。

Input Object:

{
452:{
     bla:123,
     dff:233,
     rank:2
  },
234:{
     bla:123,
     dff:233,
     rank:1
  }

}

Output Array:

[
 { bla:123, dff:233, rank:1},
 { bla:123, dff:233, rank:2 }
]

2 个解决方案

#1


8  

Example:

var data = {
    foo: {
        rank: 5
    },
    bar: {
        rank: 2
    },
    baz: {
        rank: 8
    }
};

Javascript:

var mappedHash = Object.keys( data ).sort(function( a, b ) {
    return data[ a ].rank - data[ b ].rank;
}).map(function( sortedKey ) {
    return data[ sortedKey ];
});

That would first sort the inner objects by the value of obj.rank and after that map the containing objects into an Array.

这将首先按obj.rank的值对内部对象进行排序,然后将包含的对象映射到Array中。

Result: [{rank: 2}, {rank: 5}, {rank: 8}]

结果:[{rank:2},{rank:5},{rank:8}]


Reference: Object.keys, Array.prototype.sort, Array.prototype.map

参考:Object.keys,Array.prototype.sort,Array.prototype.map


The above code contains ECMAscript 262 edition 5 code, which is available in all modern browsers. If you want to support legacy browsers as well, you need to include one of the various ES5-Shim libraries.

上面的代码包含ECMAscript 262版本5代码,可在所有现代浏览器中使用。如果您还想支持旧版浏览器,则需要包含各种ES5-Shim库中的一个。

#2


0  

Iterate over your object's properties, pushing the inner objects into an array, and then sort the array with a custom sort function:

迭代对象的属性,将内部对象推入数组,然后使用自定义排序函数对数组进行排序:

var inputObject = {}, // your object here
    arr = [];

for (var k in inputObject)
    arr.push(inputObject[k]);

arr.sort(function(a,b) { return a.rank - b.rank; });

#1


8  

Example:

var data = {
    foo: {
        rank: 5
    },
    bar: {
        rank: 2
    },
    baz: {
        rank: 8
    }
};

Javascript:

var mappedHash = Object.keys( data ).sort(function( a, b ) {
    return data[ a ].rank - data[ b ].rank;
}).map(function( sortedKey ) {
    return data[ sortedKey ];
});

That would first sort the inner objects by the value of obj.rank and after that map the containing objects into an Array.

这将首先按obj.rank的值对内部对象进行排序,然后将包含的对象映射到Array中。

Result: [{rank: 2}, {rank: 5}, {rank: 8}]

结果:[{rank:2},{rank:5},{rank:8}]


Reference: Object.keys, Array.prototype.sort, Array.prototype.map

参考:Object.keys,Array.prototype.sort,Array.prototype.map


The above code contains ECMAscript 262 edition 5 code, which is available in all modern browsers. If you want to support legacy browsers as well, you need to include one of the various ES5-Shim libraries.

上面的代码包含ECMAscript 262版本5代码,可在所有现代浏览器中使用。如果您还想支持旧版浏览器,则需要包含各种ES5-Shim库中的一个。

#2


0  

Iterate over your object's properties, pushing the inner objects into an array, and then sort the array with a custom sort function:

迭代对象的属性,将内部对象推入数组,然后使用自定义排序函数对数组进行排序:

var inputObject = {}, // your object here
    arr = [];

for (var k in inputObject)
    arr.push(inputObject[k]);

arr.sort(function(a,b) { return a.rank - b.rank; });