返回找到的对象计数 [英] return found objects count

查看:46
本文介绍了返回找到的对象计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储json对象的txt文件





 [{id:2 , 姓名: 艾哈迈德, 部门:2},{ ID:3 名称: 穆罕默德, 部门:2},{ ID:4 名称:马哈茂德  部门:3},{ ID:5 名称: 苏海南, 部门: 2},{ ID:6中, 名称: 莫纳,部门 : 1 },{ ID :7, 名称 : 纳迪亚, 部门: 2},{ ID:8中, 名称: 萨拉, 部门 : 1},{ ID:9 名称: 脉, 部门: 0},{ ID:10, 名称: 纳斯尔, 部门: 1 },{ ID :11, 姓名 : 阿拉, 部门: 3},{ ID:12, 名称: 穆巴拉克, 部门: 2 } 


代码

函数empPerDept(did){
var total = 0;
$ .getJSON(test.txt,function(emps){
for(var i = 0; i < emps.length; i ++) {

如果 (emps [i] .dept = = did) {

总++;

< span class =code-attribute> }

}

alert(total in + total); //返回 正确 结果 5 excutes 外部 总计

< span class =code-attribute> });

alert(total outside + total); // aways return 0 excutes 之前 内部 总计



}



问题 console.log 输出 undefined ,但是 count 5 里面 handler of getJson < span class =code-attribute> method




什么 问题 to solve

解决方案

.getJSON(test.txt,function(emps){
for(var i = 0; i < emps.length; < span class =code-attribute> i ++) {

if (emps [i] .dept = = did) {

< span class =code-attribute> total ++;

< span class =code-attribute> }

}

alert(total inside + total); //返回 正确 结果 5 excutes 外部 总计

});

alert(总计 <之外span class =code-attribute> +
total); // aways return 0 excutes before inside total
< span class =code-attribute>


}



问题 console.log outputs undefined ,但 count 5 inside handler getJson 方法



问题 如何 < span class =code-attribute> to solve


您的JSON字符串表示一个数组。与其他对象相比,数组有一个额外的功能:它们具有预定义属性 length

  var  length = emps.length; 



更一般地说,为什么使用

<目前尚不清楚br />

.each 可以使代码比它更重。您可以简单地写一下:

  for  var  element  in  \\ temps)
doSomethingWithElement(element); // 例如



-SA

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

解决方案

.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


Your JSON string represents an array. Arrays have one additional feature compared to other objects: they have predefined property length:

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:

for(var element in emps)
   doSomethingWithElement(element); // for example


—SA


这篇关于返回找到的对象计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆