findByScale
  • 12 Nov 2020
  • 1 Minute to read
  • Contributors
  • Dark
    Light
  • PDF

findByScale

  • Dark
    Light
  • PDF

Article summary

Description

Loops through each item in the passed array and returns the item whose value is closest to specified value without going over.


Input Parameters

NameTypeRequiredDescription
arrayAnyYesArray of Json objects
attributeTextYesProperty name whose value will be compared to the specified value
valueNumberYesNumeric value that each item is being compared to

Returns

Type: Any

Returns the Json object from the passed array whose value (at the specified attribute) was closest to the specified value.


Examples

var items = [
    { "age": 10, "name": "Ethan" },
    { "age": 20, "name": "Annabelle" },
    { "age": 30, "name": "Grant" },
    { "age": 40, "name": "Terri" },
    { "age": 50, "name": "Ben" }
];

// find the item in the array whose 'age' is closest to 25 without going over
// note: the entire object for 'Annabelle' is returned
var item = tools.findByScale(items, "age", 25);

Remarks

Use this function when you have an array of Json objects and you want to find a particular object based on a numeric value comparison with a property common to all objects in the array.


What's Next