lodash 数组的常用做法
import _ from "lodash";
const users = [
{ id: 1, name: "A", dept: "dev", score: 80 },
{ id: 2, name: "B", dept: "ops", score: 95 },
{ id: 3, name: "C", dept: "dev", score: 88 },
{ id: 3, name: "C", dept: "dev", score: 86},
];
// 分组
const byDept = _.groupBy(users, "dept");
// 建索引
//const byId = _.keyBy(users, "id");
// 排序
const sorted = _.orderBy(users, ["score"], ["desc"]);
// 安全取值
const city = _.get({ profile: { addr: { city: "Shanghai" } } }, "profile.addr.city", "N/A");
console.log(sorted);
漫思
