【发布时间】:2017-08-24 01:14:36
【问题描述】:
我目前正在尝试按姓名顺序对员工列表进行排序,但我在这样做时遇到了困难。
我正在使用 React-Native、ES6、Lodash 和 firebase。
Firebase 结构显示为:
state.employees 结构显示为:
{"-KgMYnBrqXCIPqjs0n7x":{"name":"James","phone":"123766","shift":"星期一"}, "-KgRiK6qiJsoZ_HBXt7K":{"name":"Nick"," phone":"123767","shift":"Tuesday"},"-KgRiM77VTOejvYPWPIp":{"name":"Henry","phone":"123","shift":"Thursday"},"-KgRiOaN14OeSjaYWb1O ":{"name":"Charlie","phone":"5643","shift":"Saturday"}}
并继续让更多员工拥有一个 UID,然后是姓名、电话和班次等属性。
虽然我可以使用 Lodash 提供的 _.orderBy 对列表进行排序,但在 _.map 之前应用时,似乎会删除作为 firebase 提供的键的 UID。
这是我目前有哪些订单,但 UID 已删除,_.map 工作正常,将 UID 与属性保持一致,但无序。
const mapStateToProps = state => {
const sortedEmployees = _.orderBy(
state.employees,
[employee => employee.name.toLowerCase()]
);
const employees = _.map(sortedEmployees, (val, uid) => {
return { ...val, uid };
});
return { employees };
};
非常感谢
【问题讨论】:
-
您能否用一些数据阐明 state.employees 结构的外观?是不是像
[ { kgMYnBrqXCIPqjs0n7x: { name: 'James', phone: '123766', shift: 'Monday'} }, .... ] -
感谢@agent_hunt,我已经在上面添加了 state.employees 的表示。
标签: firebase react-native firebase-realtime-database ecmascript-6 lodash