npm install array-indexof-object
var oai = require('array-indexof-object')
var arr = [ {"a":"1","b":"2"}, {"a":"5","b":"2"},
{"a":"1","b":"2"},
{"b":"7"},{"a":"3","b":"7"}]
oai.firstIndexOf(arr, { "a": "5", "b": "2" }) //Return => 1
oai.allIndexesOf(arr, { "a": "1" })//Return => [0,2]
oai.lastIndexOf(arr, { "a": "1" }) //Return => 2
oai.nthIndexOf(arr, { "b": "7" }, 2) //Return => 4
A utility which implements well known,
-
firstIndexOf
-
allIndexsOf
-
lastIndexOf
-
nthIndexOf
functionality for an object array.
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
All properties of the parameter object, is matched with objects in array to find matching obejects. This is the default behaviour.
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.
var aio = require('array-indexof-object')
//object array
var arr = [
{
"a":"1",
"b":"2"
},
{
"a":"5",
"b":"2"
},
{
"a":"1",
"b":"2"
},
{
"b":"7"
},
{
"a":"3",
"b":"7"
}
]
// firstIndexOf
aio.firstIndexOf(arr, { "a": "5", "b": "2" })
//Return => 1
aio.firstIndexOf(arr, { "a": "1" })
//Return => 0
aio.firstIndexOf(arr, { "b": "8" })
//Return => -1
// allIndexesOf
aio.allIndexesOf(arr, { "a": "5", "b": "2" })
//Return => [1]
aio.allIndexesOf(arr, { "a": "1" })
//Return => [0,2]
aio.allIndexesOf(arr, { "b": "8" })
//Return => []
// lastIndexOf
aio.lastIndexOf(arr, { "a": "5", "b": "2" })
//Return => 1
aio.lastIndexOf(arr, { "a": "1" })
//Return => 2
aio.lastIndexOf(arr, { "b": "8" })
//Return => -1
// nthIndexOf
// scan the array for {"b","7"} object.
// return the index when the object is found for the 2nd time
aio.nthIndexOf(arr, { "b": "7" }, 2)
//Return => 4
//Example Selective match
//match only the property "b" of object { "a": "5", "b": "2" }, with objects in array.
//return the index, when matched
aio.firstIndexOf(arr, { "a": "5", "b": "2" }, ["b"])
//Result=> 0 .
//this is equalant to => aio.firstIndexOf(arr, { "b": "2"})