i have txt file that store json objects
[{"id":2,"name":"ahmed","dept":2},{"id":3,"name":"Mohammed","dept":2},{"id":4,"name":"Mahmoud","dept":3},{"id":5,"name":"hanan","dept":"2"},{"id":6,"name":"mona","dept":"1"},{"id":7,"name":"nadia","dept":"2"},{"id":8,"name":"sara","dept":"1"},{"id":9,"name":"mai","dept":"0"},{"id":10,"name":"nasr","dept":"1"},{"id":11,"name":"alaa","dept":"3"},{"id":12,"name":"hosni","dept":"2"}]
the code
function empPerDept(did) {
var total = 0;
$.getJSON("test.txt", function (emps) {
for (var i = 0; i < emps.length; i++) {
if (emps[i].dept == did) {
total++;
}
}
alert("total inside" + total);//return correct result 5 excutes after outer total
});
alert("total outside" + total);//aways return 0 and excutes before inside total
}
the problem is that console.log outputs undefined ,however count is 5 inside handler of getJson method
what is problem and how to solve