Node JS - 解析对象,其中键是使用这些对象属性作为条件的对象数组
Node JS - Parse object where keys are arrays of objects using those objects properties as conditions
我有一个对象,其中每个键都是一个 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'
}
]
}
新对象的条件是return只有具有多个字符的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'
}
]
}
您可以组合使用 Object.entries、Object.fromEntries、过滤函数和集合。
const result = Object.fromEntries(
Object.entries(x)
.filter(([,data]) =>
new Set(data.filter(({character}) => character !== 'Self')).size > 1
)
)
我有一个对象,其中每个键都是一个 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'
}
]
}
新对象的条件是return只有具有多个字符的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'
}
]
}
您可以组合使用 Object.entries、Object.fromEntries、过滤函数和集合。
const result = Object.fromEntries(
Object.entries(x)
.filter(([,data]) =>
new Set(data.filter(({character}) => character !== 'Self')).size > 1
)
)