我必须使用for循环在数组项中进行搜索,而没有任何“方法”,“函数”或“ break”语法。我写的这段代码芽不起作用!
var array = [12, "Data", true, "S"]
var searchItem = "Data"
var i
for (i = 0; i < array.length; i++) {
if (array[i] == searchItem) {
console.log("Item Found")
} else {
console.log("Item Not Found")
}
}
答案 0 :(得分:1)
破译所要获取的内容有点困难,但是您是否正在寻找类似的东西?
var array = [12, "Data", true, "S"];
var searchItem = "Data";
var i;
var found = false;
for (i = 0; i < array.length; i++) {
if (array[i] == searchItem) found = true;
}
console.log(found?'Item Found':'Item Not Found');