如何根据对象属性对数组中的对象键进行排序?

时间:2019-05-09 18:11:04

标签: javascript sorting lodash

我有一个具有以下结构的对象:

root_object: {
    ITEM_ONE: {
        order: 2,
        prop_one: '123',
        prop_two: '456',
    },
    ITEM_TWO: {
        order: 1,
        prop_one: '789',
        prop_two: '321',
    },
    ITEM_THREE: {
        order: 3,
        prop_one: '654',
        prop_two: '987',
    },
}

最终,我将需要获取键列表:

['ITEM_ONE', 'ITEM_TWO', 'ITEM_THREE']

但是,需要根据属性order对其进行排序,其结果为:

['ITEM_TWO', 'ITEM_ONE', 'ITEM_THREE']

我更愿意为此目的使用_lodash库,但是我什至不知道如何开始使用它,因为root_object的结构是对象而不是列表。 / p>

6 个答案:

答案 0 :(得分:4)

您可以使用sort方法和Object.keys使用普通js来做到这一点。

const data = {"ITEM_ONE":{"order":2,"prop_one":"123","prop_two":"456"},"ITEM_TWO":{"order":1,"prop_one":"789","prop_two":"321"},"ITEM_THREE":{"order":3,"prop_one":"654","prop_two":"987"}}
const res = Object.keys(data).sort((a, b) => data[a].order - data[b].order)
console.log(res)

答案 1 :(得分:2)

只需将sort与回调函数一起使用,就像这样:

var root = {ITEM_ONE:{order:2,prop_one:'123',prop_two:'456'},ITEM_TWO:{order:1,prop_one:'789',prop_two:'321'},ITEM_THREE:{order:3,prop_one:'654',prop_two:'987'}};

var keys = Object.keys(root).sort((a, b) => root[a].order - root[b].order);
console.log(...keys);

对于lodash解决方案,请依次使用toPairssortBymap来获取密钥:

var root = {ITEM_ONE:{order:2,prop_one:'123',prop_two:'456'},ITEM_TWO:{order:1,prop_one:'789',prop_two:'321'},ITEM_THREE:{order:3,prop_one:'654',prop_two:'987'}};

console.log(..._.map(_.sortBy(_.toPairs(root), '1.order'), 0));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

答案 2 :(得分:1)

您可以通过获取要排序的值来对键进行排序。

var object = { root_object: { ITEM_ONE: { order: 2, prop_one: '123', prop_two: '456' }, ITEM_TWO: { order: 1, prop_one: '789', prop_two: '321' }, ITEM_THREE: { order: 3, prop_one: '654', prop_two: '987' } } },
    keys = Object.keys(object.root_object);
    
keys.sort((a, b) => object.root_object[a].order - object.root_object[b].order);

console.log(keys);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:1)

只需在Array.prototype.sort()的数组上使用Object.keys(root_object)

const root_object = {
    ITEM_ONE: {
        order: 2,
        prop_one: '123',
        prop_two: '456',
    },
    ITEM_TWO: {
        order: 1,
        prop_one: '789',
        prop_two: '321',
    },
    ITEM_THREE: {
        order: 3,
        prop_one: '654',
        prop_two: '987',
    },
}

let result = Object.keys(root_object).sort((a,b) => root_object[a].order - root_object[b].order);

console.log(result);

答案 4 :(得分:1)

这是O(n)解决方案(排序为O(nlogn)),仅适用于唯一的order

您可以使用lodash将每个属性的值映射到顺序({ ITEM_ONE: 2, ITEM_TWO: 1, ... }),然后反转键和值({ 1: ITEM_TWO, 2: ITEM_ONE, ... }),并提取值。这将为您提供right order中的项目:

const { flow, partialRight: pr, mapValues, invert, values } = _

const fn = flow(
  pr(mapValues, 'order'), // convert to { ITEM_ONE: 2, ITEM_TWO: 1, ... }
  invert, // convert to { 1: ITEM_TWO, 2: ITEM_ONE, ... }
  values // get the array of keys
)

const root_object = {"ITEM_ONE":{"order":2,"prop_one":"123","prop_two":"456"},"ITEM_TWO":{"order":1,"prop_one":"789","prop_two":"321"},"ITEM_THREE":{"order":3,"prop_one":"654","prop_two":"987"}}

const result = fn(root_object)
  
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

在香草JS中,通过将对象转换为条目,将条目映射为[order, key]的形式,使用Object.fromEntries()将条目转换为对象(IE / Edge不支持),然后获取值:

const fn = o => Object.values(Object.fromEntries(
  Object.entries(o).map(([k, { order }]) => [order, k])
))

const root_object = {"ITEM_ONE":{"order":2,"prop_one":"123","prop_two":"456"},"ITEM_TWO":{"order":1,"prop_one":"789","prop_two":"321"},"ITEM_THREE":{"order":3,"prop_one":"654","prop_two":"987"}}

const result = fn(root_object)
  
console.log(result)

答案 5 :(得分:0)

您可以将Object.keysArray.mapArray.sort一起使用

const root_object = {
    ITEM_ONE: {
        order: 2,
        prop_one: '123',
        prop_two: '456',
    },
    ITEM_TWO: {
        order: 1,
        prop_one: '789',
        prop_two: '321',
    },
    ITEM_THREE: {
        order: 3,
        prop_one: '654',
        prop_two: '987',
    },
}

let rootArr = [];

let sortedRootObject = Object.keys(root_object).map(key => rootArr.push(root_object[key])).sort((a, b) => rootArr[b.order] - rootArr[a.order])

console.log(sortedRootObject)