有没有更优雅的方式来编写此行代码?
_.chain(_.uniqBy(someArray,"someProperty.name")).map("someProperty").value();
似乎我应该能够链接它,而不是将lodash .uniqBy调用嵌套在.chain调用中.
我希望以下内容能奏效,但不会.
_.chain(someArray).map("someProperty").value().uniqBy("name");
输入为:
someArray = [
{
aProp: '1',
anotherProp: '2',
thirdProp: '3',
someProperty: {
id: 1,
name: "john"
}
},
{
aProp: '3',
anotherProp: '4',
thirdProp: '5',
someProperty: {
id: 1,
name: "john"
}
},
{
aProp: '2',
anotherProp: 'f',
thirdProp: '6',
someProperty: {
id: 2,
name: "tom"
}
},
{
aProp: 't',
anotherProp: 'g',
thirdProp: 'f',
someProperty: {
id: 3,
name: "yacob"
}
},
];
输出应为:
[{id:1, name:"john"},{id:2, name:"tom"},{id:3, name:"yacob"}]
解决方法:
实际上,您在正确的轨道上:
_.chain(data).map('someProperty').uniqBy('id').value()