Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have txt file that store json objects


XML
[{"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
Posted
Updated 5-Mar-15 22:13pm
v2

JavaScript
function countEmps(did) {
            var empsCount = 0;
            $.ajax({
                type: 'GET',
                url: 'test.txt',
                dataType: 'json',
                async: false,
                success: function (employess) {
                    for (var i = 0; i < employess.length; i++) {

                        if ((Number(employess[i].dept)) == (Number(did))) {
                            empsCount++;
                        }
                    }
                }
            });
            return empsCount;
        }
 
Share this answer
 
Your JSON string represents an array. Arrays have one additional feature compared to other objects: they have predefined property length:
JavaScript
var length = emps.length;

More generally, it's not clear why using $.each at all, to make the code heavier than it can be. You could simply write:
JavaScript
for(var element in emps)
   doSomethingWithElement(element); // for example

—SA
 
Share this answer
 
Comments
TheSniper105 5-Mar-15 19:17pm    
this is not what i want
i want yo get the employees count in depratment i pass to function
ex if i pass 2 to the function empPerDept then that function will return total count of employees in department 2

like select Count(*) from emp where dept=2 can u help
Sergey Alexandrovich Kryukov 5-Mar-15 19:27pm    
How it could be the different value? Okay, the execute it all through the loop, I've demonstrated how to do it. And calculate whatever you want.
—SA
TheSniper105 6-Mar-15 4:14am    
see update code the problem now is return total
Sergey Alexandrovich Kryukov 6-Mar-15 8:39am    
And what is that problem? (By the way, in most cases, it's better to use ===, not ==.)
Your result depends on the value of did.
What count 5?
—SA
TheSniper105 6-Mar-15 15:57pm    
number of employee in that department is stored in total variable (did short for department id )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900