使用嵌套数组的对象字段值javascript对数组进行分组
我有一个这样的数组
data = [
{ name : "sa", attributes : [{skin : "green"},{nose:"good"}]},
{ name : "sa", attributes : [{skin : "red"},{nose:"bad"}]},
{ name : "sa", attributes : [{skin : "green"},{nose:"good"}]},
{ name : "sa", attributes : [{nose:"good}]},
]
,所以我想要根据皮肤属性对数组进行分组。我正在使用像这样的_下划线库
const attributeType = "skin";
const groupedCollections = _.groupBy(colls, (col) => {
const data = col.attributes.find((attribute) => {
const keys = Object.keys(attribute);
return keys.indexOf(attributeType) > -1 ? true : false
});
return data?.value
});
,但这正在未定义
下进行分组。
有什么帮助吗?
I have an array like this
data = [
{ name : "sa", attributes : [{skin : "green"},{nose:"good"}]},
{ name : "sa", attributes : [{skin : "red"},{nose:"bad"}]},
{ name : "sa", attributes : [{skin : "green"},{nose:"good"}]},
{ name : "sa", attributes : [{nose:"good}]},
]
so i want the grouping of array based on skin attribute. I am using _ underscore library like this
const attributeType = "skin";
const groupedCollections = _.groupBy(colls, (col) => {
const data = col.attributes.find((attribute) => {
const keys = Object.keys(attribute);
return keys.indexOf(attributeType) > -1 ? true : false
});
return data?.value
});
but this is making grouping under undefined
.
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要返回所需属性的值。
You need to return the value of the wanted property.