简体   繁体   English

按可选字段对对象进行排序而不会丢失键

[英]Sort an object by an optional field without losing the keys

I've got the following object which I'd like to sort by its priority fields.我有以下对象,我想按其优先级字段对其进行排序。

{
    anchor_block: {
        label: 'Anker-Block',
        priority: 100,
        description: ...,
        fields: ...
    },
    categories_block: {
        label: 'Categories-Block',
        description: ...,
        fields: ...
    },
    contact_block: {
        label: 'Kontakt-Block',
        description: ...,
        fields: ...
    },
    employee_block: {
        label: 'Mitarbeiter-Block',
        priority: 90,
        description: ...,
        fields: ...
    }
}

I've tried to sort them with lodash:我试图用 lodash 对它们进行排序:

_.orderBy(blocks, ['priority'], ['desc']);

But this solution removes the all the keys from my objects (because it returns an array) and it also puts the ones without a priority field on top:但是此解决方案从我的对象中删除了所有键(因为它返回一个数组),并且还将没有优先级字段的键放在最上面:

[
    {
        label: 'Categories-Block',
        description: ...,
        fields: ...
    },
    {
        label: 'Kontakt-Block',
        description: ...,
        fields: ...
    },
    {
        label: 'Anker-Block',
        priority: 100,
        description: ...,
        fields: ...
    },
    {
        label: 'Mitarbeiter-Block',
        priority: 90,
        description: ...,
        fields: ...
    },
]

Does any one of you have an idea on how to solve this with lodash?你们中有人知道如何用 lodash 解决这个问题吗? I'd like to avoid iterating it myself, because I'm sure it's possible, I just don't get my head around it.我想避免自己迭代它,因为我确定这是可能的,我只是不明白它。

Updated Answer: Approach is to store all keys, sort them according to the order you desire, and then use those keys to retrieve values from the original object in desired order.更新的答案:方法是存储所有键,根据您想要的顺序对它们进行排序,然后使用这些键以所需的顺序从原始对象中检索值。

let obj = {
    anchor_block: {
        label: 'Anker-Block',
        priority: 100,
        description: '...',
        fields: '...'
    },
    categories_block: {
        label: 'Categories-Block',
        description: '...',
        fields: '...'
    },
    contact_block: {
        label: 'Kontakt-Block',
        description: '...',
        fields: '...'
    },
    employee_block: {
        label: 'Mitarbeiter-Block',
        priority: 90,
        description: '...',
        fields: '...'
    }
}
/*
    Plain JS, no Lodash
*/

let sortedKeys = Object.keys(obj);
sortedKeys.sort((key1, key2) => {
    let p1 = obj[key1].priority || 0, p2 = obj[key2].priority || 0;
    if(p1<p2)
        return 1; // Put key2 first in sorted order
    return -1; // Else, put key1 first in sorted order
    /*
        For ascending order, do this
        if(p1<p2)
            return -1;
        return 1;
    */
})

sortedKeys.forEach(key => {
    console.log(key,"-->",obj[key]);
})

Output输出

anchor_block --> { label: 'Anker-Block',
  priority: 100,
  description: '...',
  fields: '...' }
employee_block --> { label: 'Mitarbeiter-Block',
  priority: 90,
  description: '...',
  fields: '...' }
categories_block --> { label: 'Categories-Block', description: '...', fields: '...' }
contact_block --> { label: 'Kontakt-Block', description: '...', fields: '...' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM