indexOf is not wokring during search the object in array in ionic3?


i have to get the indexOf selected Object but i got every time return -1 tell me whats the wrong in my code?

	addOrRemoveSubcategory(event:any, subCategroyObject: any) {
		//For firestore - Here we will hold the subcateoryObject and push into array 
		this.checkedCategoryObject = {
			"id"                 : subCategroyObject.keyId,
			"subCategoryName"    : subCategroyObject.subCategoryName
		if(event.checked) {
			this.selectedSubCategoryKeyId = subCategroyObject.keyId;
			//Push only if not eixst in the array
			if(this.selectedSubCategoryArray.indexOf( == -1){
		} else {
			this.selectedSubCategoryKeyId = subCategroyObject.keyId;
			var index         = this.selectedSubCategoryArray.indexOf(this.checkedCategoryObject);
			this.selectedSubCategoryArray.splice(index, 1);


You search for an id while your array is a collection of object, so it tries to compare an object to an number => will never works, product -1.
You should use findIndex as follows:

let m = [{key:2,val:‘aaa’},{key:5,val:‘bbb’}];
let i = m.findIndex(item=>{return item.key==5});
console.log('i: ',i);


sir i m using this but i got error findIndex of undefined
var i= this.selectedSubCategoryArray.findIndex(item=>{return});


I can only guess that this member variable (this.selectedSubCategoryArray) was never initialized with [] or new Array.
In your constructor code, initialize this member variable.


Would suggest u to use lodash


Sir i have already initialized array in the constructor ?


If you wish, you can share you code, and we can take a look.


Should be:
var i= this.selectedSubCategoryArray.findIndex(item=>{return});