请对键和值的映射进行排序,请订购带有Lodash的ES6中的列表(保持Firebase键:UID)? [英] Sorting a map of keys and values please to order a list in ES6 with Lodash (Keeping Firebase Key: UID)?

查看:44
本文介绍了请对键和值的映射进行排序,请订购带有Lodash的ES6中的列表(保持Firebase键:UID)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试按姓名顺序对员工列表进行排序,但是我很难做到这一点.

I'm currently trying to sort a list of employees in order of names but I'm having difficulty in such.

我正在使用带有ES6,Lodash和firebase的React-Native.

I'm using React-Native, with ES6, Lodash and firebase.

Firebase结构显示为:

Firebase structure appears as:

state.employees结构显示为:

The state.employees structure appears as:

{-KgMYnBrqXCIPqjs0n7x":{"name":"James","phone":"123766","shift":"Monday"},"-KgRiK6qiJsoZ_HBXt7K":{"name":"Nick","phone:" 123767," shift:" Tuesday},"-KgRiM77VTOejvYPWPIp:{" name:" Henry," phone:" 123," shift:" Thursday},"-KgRiOaN14OeSjaYWb1O:{" name:" Charlie," phone:" 5643," shift:" Saturday}}

{"-KgMYnBrqXCIPqjs0n7x":{"name":"James","phone":"123766","shift":"Monday"}, "-KgRiK6qiJsoZ_HBXt7K":{"name":"Nick","phone":"123767","shift":"Tuesday"},"-KgRiM77VTOejvYPWPIp":{"name":"Henry","phone":"123","shift":"Thursday"},"-KgRiOaN14OeSjaYWb1O":{"name":"Charlie","phone":"5643","shift":"Saturday"}}

并继续增加更多的员工,每个员工都有一个UID,然后有诸如姓名,电话和轮班之类的属性.

and continues with more employees each having a UID and then properties such as name, phone and shift.

尽管我可以使用Lodash提供的_.orderBy来排序列表,但似乎这样做时,在_.map之前应用时,将删除作为Firebase提供的键的UID.

Although I am able to order the list with _.orderBy provided by Lodash, it appears that when doing so the UID as the key provided by firebase is removed when applied say before _.map.

这是我当前有哪些命令,但是删除了UID,_.map可以很好地将UID与属性保持一致,但是无序.

This is what I currently have which orders but the UID is removed, the _.map works fine keeping the UID with the properties but is unordered.

const mapStateToProps = state => {    
    const sortedEmployees = _.orderBy(
        state.employees, 
        [employee => employee.name.toLowerCase()]
    );
    const employees = _.map(sortedEmployees, (val, uid) => {
        return { ...val, uid };
    });

    return { employees };
};

非常感谢

推荐答案

_.orderBy(collection,[iteratees = [_.identity]],[orders]) 当lodash映射到Object类型的集合时,它似乎省略了它的键.修复的一种方法是将对象集合从firebase响应转换为数组.例如:

It seems when lodash maps over collection of type Object, it omits its key. One way to fix would be to convert the collection of object from firebase response to an array . Ex:

const arrWithKeys = Object.keys(state.employees).map((key) => {
    return {
      key,
      ...state.employees[key]
    }
});

然后使用

const sortedEmployees = _.orderBy(
        arrWithKeys, 
        [employee => employee.name.toLowerCase()]
    );

您还可以在Firebase查询中使用Firebase排序

You can also use firebase sorting in your firebase query

https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data

这篇关于请对键和值的映射进行排序,请订购带有Lodash的ES6中的列表(保持Firebase键:UID)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆