jQuery中的$ .each函数是否有条件循环 [英] Is there conditional looping with $.each function in jQuery

查看:154
本文介绍了jQuery中的$ .each函数是否有条件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于jQuery $.each方法的查询.下面是我的ajax,效果很好:

I have a query about jQuery's $.each method. Below is my ajax which is working quite well:

$.ajax({
    url:'js/people-json.js',
    type:'post',
    dataType:'json',
    success:function(data){
        $.each(data.names, function(i, data) {
            console.log(data);
        });
    },
    error:function(err){
        console.log(err);
    }
});

此脚本运行良好,并向我提供了我想要获取的JSON结果.但这给了我结果,但这是我存储在外部JS文件中的一长串信息,该文件具有JSON格式,如下所示:

This script is working fine and giving me the JSON results which I wanted to get. However this is giving me results but this is a long list of information I have stored in an external JS file with a JSON format that looks like this:

{
"people": [
    {
        "name": "aaa",
        "age": 32,
        "email": "aaa@abc.xyz"
    },
    {
        "name": "bbb",
        "age": 21,
        "email": "bbb@abc.xyz"
    },
    {
        "name": "ccc",
        "age": 45,
        "email": "ccc@abc.xyz"
    },
    ..............lot of more here around 8000
  ]
}

是否可以通过以下方式在jQuery中循环:

Is there way to loop in jQuery either this way:

$.each(data.names<=200, function(i, data) {
    console.log(data);
});

或这种方式:

$.each(data.names, function(i<=200, data) {
    console.log(data);
});

或这个:

$.each(data.names, function(i, data<=200) {
    console.log(data);
});

我们能否在第一次加载时循环获得200个结果 单击另一个200的按钮循环,依此类推.

Can we loop with 200 results at first load and then with click of a button loop with another 200 and so on.

注意:为此,我希望使用jQuery解决方案.

Note: I would prefer jQuery solution for this.

推荐答案

直接回答您的问题,否

.each()迭代集合中的所有项目.它不提供过滤选项.您将像这样过滤自己,并为每次点击更改if语句中的值:

.each() iterates all items in the collection. It does not offer a filtering option. You will just filter yourself like this and change the value in the if statement for each click:

$.each(data, function(i, data) {
    if (i <= 200) {
        console.log(data);
    }
});

或者,如果data是一个数组,则可以提取前200个并对其进行迭代:

Or, if data is an array, you could extract the first 200 and iterate them:

$.each(data.slice(0, 200), function(i, data) {
    console.log(data);
});

或者,如果data是数组,则for循环将为您提供更大的灵活性:

Or, if data is an array, a for loop will give you more flexibility:

for (var i = 0; i < 200; i++) {
    console.log(data[i]);
}

如果您尝试每次单击执行200个项目,那么您需要解释更多有关您正在做什么的信息.您是否从ajax调用中获得所有结果?如果是这样,您就不想每次都重新获取它们.只需存储所有结果并每次处理下一个200.保留一个计数器变量,指示您到目前为止已处理了多少.

If you're trying to do 200 items each click, then you need to explain more about what you're doing. Are you getting all results from the ajax call? If so, they you don't want to be refetching them each time. Just store all the results and process the next 200 each time. Keep a counter variable that indicates how many you've processed so far.

var data = [...];
var dataIterationCnt = 0;

function getNextChunk(n) {
    var end = Math.min(dataIterationCnt + n, data.length)
    for (var i = dataIterationCnt; i < end; i++) {
        console.log(data[i]);
    }
    dataIterationCnt = i;
    return(dataIterationCnt < data.length);
}

// then in your code, you just call getNextChunk(200)
// until it returns null which means it has no more to iterate
var more = getNextChunk(200);

这篇关于jQuery中的$ .each函数是否有条件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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