Node JS - 解析对象,其中键是使用这些对象属性作为条件的对象数组
我有一个对象,其中每个键都是一个 Actor 名称,其属性是一组包含一些信息的对象,如下所示:
const x = {
'Jonathan Majors': [
{
character: 'Kang the Conqueror',
title: 'Ant-Man and the Wasp: Quantumania',
release_date: '2023-07-26'
},
{
character: 'He Who Remains (archive footage)',
title: "Marvel Studios' 2021 Disney+ Day Special",
release_date: '2021-11-12'
}
],
'Vin Diesel': [
{
character: 'Groot (voice)',
title: 'Guardians of the Galaxy Vol. 3',
release_date: '2023-05-03'
},
{
character: 'Self',
title: 'Marvel Studios: Assembling a Universe',
release_date: '2014-03-18'
}
]
}
新对象的条件是仅返回具有多个角色的 Actor,不包括角色 Self 。因此,如果 Actor 有两个字符,但其中一个是 self,则应将其删除。
const result = {
'Jonathan Majors': [
{
character: 'Kang the Conqueror',
title: 'Ant-Man and the Wasp: Quantumania',
release_date: '2023-07-26'
},
{
character: 'He Who Remains (archive footage)',
title: "Marvel Studios' 2021 Disney+ Day Special",
release_date: '2021-11-12'
}
]
}
I have an Object where each key is an Actor name and its property is an array of objects with some infos, like below:
const x = {
'Jonathan Majors': [
{
character: 'Kang the Conqueror',
title: 'Ant-Man and the Wasp: Quantumania',
release_date: '2023-07-26'
},
{
character: 'He Who Remains (archive footage)',
title: "Marvel Studios' 2021 Disney+ Day Special",
release_date: '2021-11-12'
}
],
'Vin Diesel': [
{
character: 'Groot (voice)',
title: 'Guardians of the Galaxy Vol. 3',
release_date: '2023-05-03'
},
{
character: 'Self',
title: 'Marvel Studios: Assembling a Universe',
release_date: '2014-03-18'
}
]
}
The condition to the new object is to return only the Actors that have more than one character, excluding the character Self .. so if the Actor has two chars, but one is self, it should be deleted
const result = {
'Jonathan Majors': [
{
character: 'Kang the Conqueror',
title: 'Ant-Man and the Wasp: Quantumania',
release_date: '2023-07-26'
},
{
character: 'He Who Remains (archive footage)',
title: "Marvel Studios' 2021 Disney+ Day Special",
release_date: '2021-11-12'
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Object.entries、Object.fromEntries、过滤器函数和集合的组合。
You can use a combination of Object.entries, Object.fromEntries, filter functions and a set.