我有一個物件,其中每個鍵都是一個 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'
}
]
}
uj5u.com熱心網友回復:
您可以使用 Object.entries、Object.fromEntries、過濾器函式和集合的組合。
const result = Object.fromEntries(
Object.entries(x)
.filter(([,data]) =>
new Set(data.filter(({character}) => character !== 'Self')).size > 1
)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/446041.html
