Here is my array of objects:
$scope.choices = [
{
id: 0,
product: [{id:'0'}]
},
{
id: 10,
product: [{id:'5'}]
}
];
How to find index number for id value "10"?
Let following is the object you want to search in an array
var searchObject={
id: 0,
product: [{id:'0'}]
};
Now using following
$scope.choices.indexOf(searchObject);
Hope this helps :)
if you like to use pure javascript andno problem to use the you can use following
function findWithAttr(array, attr, value) {
for(var i = 0; i < array.length; i += 1) {
if(array[i][attr] === value) {
return i;
}
}
return -1;
}
findWithAttr(choices, 'id', 10);
Alternatively for angular you can use the following function
$scope.findIndex=function(){
angular.forEach( $scope.choices,function(object,index){
console.log(object);
if(object.id==10){
console.log('index'+index);
}
})
};
You could use Object.keys() to retrieve all the keys of you're object then iterate on them to find if there is one that has the corresponding value.