Quick
npm install array-indexof-object
var oai = var arr = "a":"1""b":"2" "a":"5""b":"2" "a":"1""b":"2" "b":"7""a":"3""b":"7" oai //Return => 1 oai//Return => [0,2] oai //Return => 2 oai //Return => 4
About
A utility which implements well known,
-
firstIndexOf
-
allIndexsOf
-
lastIndexOf
-
nthIndexOf
functionality for an object array.
Syntax
1) firstIndexOf (array, obj, optional : matchedBy )
2) allIndexesOf (array, obj, optional : matchedBy )
3) lastIndexOf (array, obj, optional : matchedBy )
4) nthIndexOf (array, obj, n ,optional : matchedBy )
* matchedBy => Refer 'selective match' section below
Features
Full match
All properties of the parameter object, is matched with objects in array to find matching obejects. This is the default behaviour.
Selective match
Only a set of properties of the parameter object, is matched with objects in array to find matching obejects. This property set could be given in optional 'matchedBy' array argument.
Examples
var aio = //object arrayvar arr = "a":"1" "b":"2" "a":"5" "b":"2" "a":"1" "b":"2" "b":"7" "a":"3" "b":"7" // firstIndexOf aio//Return => 1aio//Return => 0aio //Return => -1 // allIndexesOf aio //Return => [1]aio//Return => [0,2]aio //Return => [] // lastIndexOf aio//Return => 1aio//Return => 2aio //Return => -1 // nthIndexOf // scan the array for {"b","7"} object.// return the index when the object is found for the 2nd timeaio//Return => 4 //Example Selective match//match only the property "b" of object { "a": "5", "b": "2" }, with objects in array.//return the index, when matchedaio//Result=> 0 . //this is equalant to => aio.firstIndexOf(arr, { "b": "2"})