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
  • 141,914
  • 21
  • 225
  • 223
matieva
  • 127
  • 5
  • 1
    const foundDuplicateName = []; TestArray.forEach((nnn, index) => { foundDuplicateName.push(TestArray.find((x, ind) => x.place === nnn.place && index !== ind)); }); – Syed Aqeel Ashiq Apr 20 '22 at 05:52

0 Answers0