Skip to content

Latest commit

 

History

History
54 lines (39 loc) · 1.54 KB

findFirstByValue.md

File metadata and controls

54 lines (39 loc) · 1.54 KB

Back to reference

findFirstByValue(data, prop, val)

Returns the first object in data matching a given value val for a given property prop. This is useful when searching for specific objects in an array of objects with a unique property value.

Examples:

If you have data like this:

let superheroes = [
  {Name: "Steppenwolf", Alignment: "bad", Gender: "Male", Height: 183, Weight: 91},
  {Name: "Storm", Alignment: "good", Gender: "Female", Height: 180, Weight: 57},
  {Name: "Sunspot", Alignment: "good", Gender: "Male", Height: 173, Weight: 77},
  {Name: "Superboy", Alignment: "good", Gender: "Male", Height: 170, Weight: 68},
  {Name: "Superboy-Prime", Alignment: "bad", Gender: "Male", Height: 180, Weight: 77},
  {Name: "Supergirl", Alignment: "good", Gender: "Female", Height: 165, Weight: 54},
  {Name: "Superman", Alignment: "good", Gender: "Male", Height: 191, Weight: 101}
]
Example 1:

This will find and return the "Supergirl" object:

let supergirl = gmynd.findFirstByValue(superheroes, "Name", "Supergirl");

This is the result:

{
  Name:"Supergirl", Alignment: "good", Gender: "Female", Height: 165, Weight: 54
}
Example 2:

If there are several objects matching the given criteria, only the first object found in data is returned:

let firstGoodHero = gmynd.findFirstByValue(superheroes, "Alignment", "good");

This is the result:

{
  Name:"Storm", Alignment: "good", Gender: "Female", Height: 180, Weight: 57
}