我需要一些帮助来获取具有重复值的对象键. 我所拥有的是Object namenumn中的大约numn00个键(这是示例)

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对象中,则会将其添加.这种略微简化的方法减少了代码量,但保留了有效解决问题的核心逻辑.

结果是:

{
  '1514': 'The Rise of an Empire(帝国崛起)',
  '3140': 'A leisurely life in another world(异界的悠闲生活)',
  '5117': 'The rise of an empire(帝国崛起)',
  '5148': 'The Rise of an Empire(帝国崛起)',
  '13140': 'A leisurely life in another world(异界的悠闲生活)'
}

Javascript相关问答推荐

如何保持子画布元素的1:1宽高比?

获取加载失败:获取[.]添加时try 将文档添加到Firerestore,Nuxt 3

如何解决这个未能在响应上执行json:body stream已读问题?

使用JavaScript单击上一个或下一个特定按钮创建卡滑动器以滑动单个卡

被CSS优先级所迷惑

Javascript,部分重排序数组

扫描qr code后出错whatter—web.js

如何找出摆线表面上y与x相交的地方?

如何在angular中从JSON值添加动态路由保护?

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

不能将空字符串传递给cy.containes()

在运行时使用Next JS App Router在服务器组件中运行自定义函数

扩展类型的联合被解析为基类型

如何在JAVASCRIPT中合并两组对象并返回一些键

MarkLogic-earch.suggest不返回任何值

Next.js中的服务器端组件列表筛选

基于产品ID更新条带产品图像的JavaScript命中错误

为什么我不能使用其同级元素调用和更新子元素?

我为什么要使用回调而不是等待?

通过解构/功能组件接收props-prop验证中缺少错误"