Search code examples
javascriptobject

How to get duplicate values with different keys in One Object Javascripts?


I need some help to get Object keys that have duplicate values. What i have is about 10000 keys in Object name numn (This is example)

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(钢铁雄心之舰男穿越记)",
}

What i want is to get all duplicate values with its keys like this

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(异界的悠闲生活)",
}

I tried this

let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) !== index)
Duplicates=findDuplicates(Object.values(numn))

but it only get duplicate values without it keys

Duplicates=[
'The Rise of an Empire(帝国崛起)',
'A leisurely life in another world(异界的悠闲生活)'
]

Please help me and Sorry for my bad writing in English


Solution

  • 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));
    

    This function iterates over the keys of your original object and filters the values directly to check if the current value exists more than once among all values (case-insensitively). If a duplicate is found and it hasn't been added to the duplicates object yet, it gets added. This slightly simplified approach reduces the amount of code but keeps the core logic to solve the problem effectively.

    And the result is:

    {
      '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(异界的悠闲生活)'
    }