0

Array looks list :

[
   {
      "Name":"S",
      "Level":"1",
      "Uid":"huybd776",
      "isHuman":false
   },
   {
      "Name":"R",
      "Level":"35",
      "Uid":"673bjhbjhdcsy",
      "isHuman":true
   }
]

I have a value i.e Uid 673bjhbjhdcsy, how do I check if that Uid exists in the array and get the whole object associated with the Uid.

Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69

2 Answers2

1

You can use find like:

const data = [
   {
      "Name":"S",
      "Level":"1",
      "Uid":"huybd776",
      "isHuman":false
   },
   {
      "Name":"R",
      "Level":"35",
      "Uid":"673bjhbjhdcsy",
      "isHuman":true
   }
];

console.log(data.find(x => x.Uid === '673bjhbjhdcsy'));

Reference:

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
-1
result = [
   {
      "Name":"S",
      "Level":"1",
      "Uid":"huybd776",
      "isHuman":false
   },
   {
      "Name":"R",
      "Level":"35",
      "Uid":"673bjhbjhdcsy",
      "isHuman":true
   }
].find(item => item.Uid === '673bjhbjhdcsy')
Steve Tomlin
  • 3,391
  • 3
  • 31
  • 63