我正在尝试构建一个显示列表中第一个数字的面板,但找不到$.each
替代项。
当前,我有一段代码可以显示localStorage
中的所有项目。是否有人想到jQuery
函数,该函数将数组作为参数并仅回调第一项?与此类似-$.each( array, callback )
。
const clients = JSON.parse(localStorage.getItem("data"));
const odontologas = clients.Odontologas;
$.each(odontologas, function(i, item) {
if (item.Būsena === "Eilėje") {
$("#odontologasQueue").append(
`<p class="boardNumber">${item.EilėsNr}</p>`
);
} else {
$("#odontologasQueue").append(`<p class="boardNumber">000</p>`);
}
});
答案 0 :(得分:0)
只需将return false
添加到if
语句中即可。
答案 1 :(得分:0)
因此,当您找到匹配项时退出循环或使用array.find
var first = odontologas.find(item => item.Būsena === "Eilėje")
// var first = odontologas.find(function(item) { return item.Būsena === "Eilėje" })
console.log(first)
答案 2 :(得分:0)
长话短说,您想要条目索引吗?是
odontologas.indexOf("Eilėje"); // equals -1 if element isn't found in the array