例如,假设我在数组中有一些用户对象:
[{name: "Bob", ID: "123", location: "Texas"},
{name: "Jill", ID: "124", location: "New York"},
{name: "Jan", ID: "125", location: "Alabama"}]
我希望只获取ID并将它们放在一个新数组中,其中包含一些其他变量,这些变量将在其他位置检索,但对应于该ID,例如:
[{ID: "123", title: "Engineer"},
{ID: "124", title: "Architect"},
{ID: "125", title: "Manager"}]
我如何用Lodash实现这一目标?
答案 0 :(得分:2)
如果解决方案必须一直使用lodash,那么请忽略这一点 否则...
完成此操作
var persons = [
{name: "Bob", ID: "123", location: "Texas"},
{name: "Jill", ID: "124", location: "New York"},
{name: "Jan", ID: "125", location: "Alabama"}
];
var result = persons.map(function(person) {
return {
ID: person.ID,
title: "" /* get title */
};
});
console.log(result);