findByRange
  • 11 Jan 2021
  • 1 Minute to read
  • Contributors
  • Dark
    Light
  • PDF

findByRange

  • Dark
    Light
  • PDF

Article summary

Description

Loops through each item in the passed array and returns the item that has a range that the specified value falls within.


Input Parameters

NameTypeRequiredDescription
arrayAnyYesArray of Json objects
attrRange1TextYesProperty name whose numeric value indicates the start of the range
attrRange2TextYesProperty name whose numeric value indicates the end of the range
valueNumberYesNumeric value that is checked whether or not it is within each item's numeric range

Returns

Type: Any

Returns the Json object from the passed array whose range the specified value falls within.


Examples

var items = [
    { "value1": 0, "value2": 19, "color": "Red" },
    { "value1": 20, "value2": 29, "color": "Green" },
    { "value1": 30, "value2": 39, "color": "Blue" },
    { "value1": 40, "value2": 49, "color": "Yellow" },
    { "value1": 50, "value2": 100, "color": "Orange" }
];

// find the item in the array whose range 32 falls within
// note: the entire object for 'Blue' is returned
var item = tools.findByRange(items, "value1", "value2", 32);

Remarks

Use this function when you have an array of Json objects and you want to find a particular object based on whether or not a specified value falls within a numeric range identified by two specified properties that exist on every object in the array.


What's Next