我需要一些帮助来获取具有重复值的对象键。 我所拥有的是 Object name numn 中大约 10000 个键(这是示例)
numn={
"28": "The invincible special forces who returned to the Three Kingdoms(回到三国的无敌特种兵)",
"46": "Three Kingdoms: The opening incorporates Li Cunxiao(三国:开局融合了李存孝)",
"62": "Douluo: Super Beast Armed Guard Zhu Zhuqing(斗罗:超兽武装守护朱竹清)",
"76": "The Oriental Cavalry that swept across the Three Kingdoms(横扫三国的东方铁骑)",
"1514": "The Rise of an Empire(帝国崛起)",
"3140": "A leisurely life in another world(异界的悠闲生活)",
"5117": "The rise of an empire(帝国崛起)",
"5127": "After eavesdropping on my voice, the whole family went crazy(偷听我心声后,全家都杀疯了)",
"5148": "The Rise of an Empire(帝国崛起)",
"8440": "A ghost calls at the door in the middle of the night(夜半鬼叫门)",
"13140": "A leisurely life in another world(异界的悠闲生活)",
"13154": "The time travel story of Iron Ambition Man(钢铁雄心之舰男穿越记)",
}
我想要的是像这样获取所有重复的值及其键
numn={
"1514": "The Rise of an Empire(帝国崛起)",
"5117": "The rise of an empire(帝国崛起)",
"5148": "The Rise of an Empire(帝国崛起)",
"3140": "A leisurely life in another world(异界的悠闲生活)",
"13140": "A leisurely life in another world(异界的悠闲生活)",
}
我试过这个
let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) !== index)
Duplicates=findDuplicates(Object.values(numn))
但它只得到重复的值而没有它的键
Duplicates=[
'The Rise of an Empire(帝国崛起)',
'A leisurely life in another world(异界的悠闲生活)'
]
请帮助我并 抱歉我的英文写得不好
function findDuplicatesSimple(obj) {
let duplicates = {};
let values = Object.values(obj);
Object.keys(obj).forEach((key) => {
// Check if the value appears more than once and hasn't been added to duplicates yet
if (
values.filter((v) => v.toLowerCase() === obj[key].toLowerCase()).length >
1
) {
duplicates[key] = obj[key];
}
});
return duplicates;
}
const numn = {
28: "The invincible special forces who returned to the Three Kingdoms(回到三国的无敌特种兵)",
46: "Three Kingdoms: The opening incorporates Li Cunxiao(三国:开局融合了李存孝)",
62: "Douluo: Super Beast Armed Guard Zhu Zhuqing(斗罗:超兽武装守护朱竹清)",
76: "The Oriental Cavalry that swept across the Three Kingdoms(横扫三国的东方铁骑)",
1514: "The Rise of an Empire(帝国崛起)",
3140: "A leisurely life in another world(异界的悠闲生活)",
5117: "The rise of an empire(帝国崛起)",
5127: "After eavesdropping on my voice, the whole family went crazy(偷听我心声后,全家都杀疯了)",
5148: "The Rise of an Empire(帝国崛起)",
8440: "A ghost calls at the door in the middle of the night(夜半鬼叫门)",
13140: "A leisurely life in another world(异界的悠闲生活)",
13154: "The time travel story of Iron Ambition Man(钢铁雄心之舰男穿越记)",
};
console.log(findDuplicatesSimple(numn));
此函数迭代原始对象的键并直接过滤值以检查当前值在所有值中是否存在多次(不区分大小写)。如果找到重复项并且尚未将其添加到
duplicates
对象中,则会添加它。这种稍微简化的方法减少了代码量,但保留了有效解决问题的核心逻辑。