-2

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"?

0

5 Answers 5

0

you can use this:

var reqIndex;
$scope.choices.forEach(function(choice, index) {
    if (choice.id === 10) {
        reqIndex= index;
        return
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0

try this

$scope.choices.findIndex(function(x) {
   return x.id==10;
});

Comments

0

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 :)

1 Comment

0

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);
                    }


                })


        };

Comments

-2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.