- Print
- DarkLight
- PDF
Article summary
Did you find this summary helpful?
Thank you for your feedback
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
Name | Type | Required | Description |
---|---|---|---|
array | Any | Yes | Array of Json objects |
attribute | Text | Yes | Property name whose value will be compared to the specified value |
value | Number | Yes | Numeric 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.