1

How can I make it so this function returns all the duplicates and not only the first it finds? Right now this returns only Sweden but I would like it to return [Sweden,Denmark] since they both have duplicate values?

const TestArray = [{
    "place": "Sweden",
    "value": 5
  },
  {
    "place": "Sweden",
    "value": 15
  },
  {
    "place": "Norway",
    "value": 5
  },
  {
    "place": "Denmark",
    "value": 15
  },
  {
    "place": "Sweden",
    "value": 5
  },
  {
    "place": "Denmark",
    "value": 5
  }
]
const foundDuplicateName = TestArray.find((nnn, index) => {
  return TestArray.find((x, ind) => x.place === nnn.place && index !== ind)
})
console.log(foundDuplicateName)
Phil
  • 157,677
  • 23
  • 242
  • 245
matieva
  • 365
  • 4
  • 12
  • 1
    const foundDuplicateName = []; TestArray.forEach((nnn, index) => { foundDuplicateName.push(TestArray.find((x, ind) => x.place === nnn.place && index !== ind)); }); – Aqeel Ashiq Apr 20 '22 at 05:52

0 Answers0