gpt4 book ai didi

jquery - 如何限制jquery中json数据的each循环?

转载 作者:行者123 更新时间:2023-12-03 22:28:19 25 4
gpt4 key购买 nike

有没有一种好的方法可以使用 jquery every 循环仅循环 json 对象的前 3 项?

我正在考虑类似 .slice(start,end) 的内容功能。

var data = [ 
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "ee"},
{"Id": 10089, "PageName": "dd"},
{"Id": 10095, "PageName": "hh"}
];

$.each(data, function(i, item) {
alert(item.PageName);
// somehow break at item 3
});​

最佳答案

var data = [ 
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "ee"},
{"Id": 10089, "PageName": "dd"},
{"Id": 10095, "PageName": "hh"}
];

$.each(data, function(i, item) {
alert(item.PageName);
return i<2;
});​

当你返回 false 时,每个都会停止。

来自文档:

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

关于jquery - 如何限制jquery中json数据的each循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12648063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com