Created
April 9, 2014 10:34
-
-
Save rxw1/10252749 to your computer and use it in GitHub Desktop.
Find array index by object property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find trip array index by trip id | |
var findById = function (source, trip_id) { | |
var length = source.length; | |
for (var i = length - 1; i >= 0; --i) { | |
if (source[i].trip_id === trip_id) { | |
return i; | |
} | |
} | |
throw "Couldn't find trip with id: " + trip_id; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better:
$rootScope.trips.indexOf(trip)
.