I am working on Angular 2 with type script and came up with a very peculiar problem. I have a number array named empIds as
empIds = {1,5,6,9,2}
I have (a JSON Array) named empDetails (please note employeeId is string here)as
empDetails [{"employeeId":"1","employeeName":"Jhon"},
{"employeeId":"2","employeeName":"Ron"},
{"employeeId":"3","employeeName":"Van"}]
Now i have to find out the only JSON objects who's id match in number array. Here is what i wrote
for(let i=0;i<empDetails.length;i++){
if(empIds.indexOf(empDetails[i].employeeId)==0 ){
//Employee ID is present save that JSON object to some other JSON Array
i should get details for matched id (1 and 2)
}else{
//Did not match
do nothing
}
}
i did put console.log for both if and else. My loop never matches it always goes to else condition? Is there any possible solution?
empIds = {1,5,6,9,2}is not valid. It should beempIds = [1,5,6,9,2]. Change your condition fromif(empIds.indexOf(empDetails[i].employeeId)==0 ){toif(empIds.indexOf(empDetails[i].employeeId) >= 0 ){