如何使用JavaScript循环遍历数组中的所有条目?
我以为它是这样的:
forEach(instance in theArray)
Run Code Online (Sandbox Code Playgroud)
theArray
我的阵列在哪里,但这似乎是不正确的.
在Java中,您可以使用for
循环遍历数组中的对象,如下所示:
String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray)
{
// Do something
}
Run Code Online (Sandbox Code Playgroud)
你能用JavaScript做同样的事吗?
我被告知不要for...in
在JavaScript中使用数组.为什么不?
$(document).ready(function(){
createForm("text,password",".content");
});
function createForm(types,object){
typ = types.split(',');
//var source = "";
$.each(typ,function(){
switch(this){
case "text":
console.log('text');break;
default: console.log('default');break;
}
});
//$(object).html(source);
}
Run Code Online (Sandbox Code Playgroud)
我在控制台中有这个代码它返回2xdefaults.为什么?
我尝试将每种类型的输入作为文本或密码返回,但我的开关无法识别"typ"
我正在研究 Jquery 中学生列表的搜索功能,虽然我可以获得正确的结果以打印到控制台,但结果没有出现在页面上。匹配时,我在控制台中收到以下错误。我只是不确定我在选择器上声明 listStudents 变量的方式是否有问题?是不是不可能在这样的选择器数组上运行 each 函数?我难住了。
Uncaught TypeError: listName.each is not a function
at showPage (main.js:10)
at searchList (main.js:92)
at HTMLButtonElement.<anonymous> (main.js:54)
Run Code Online (Sandbox Code Playgroud)
下面是我的 JS 文件
var listStudents = $(".student-list").children();
var numStudents = listStudents.length;;
function showPage(pageNum, listName) {
// first hide all students on the page
$(".student-list li").hide();
pageNum = parseInt(pageNum);
// Then loop through all students in our student list argument
listName.each(function(index){
// if student should be on this page number
if ((index >= ((pageNum*10)-10)) && (index <= (pageNum*10))) …
Run Code Online (Sandbox Code Playgroud)