gpt4 book ai didi

Javascript Array 长度为 0 的非空数组

转载 作者:行者123 更新时间:2023-12-03 05:08:27 24 4
gpt4 key购买 nike

我正在使用knockout js 创建一个具有搜索功能的列表。

console.log结果:

enter image description here

正如你所看到的,数组中应该有 5 个对象,但原型(prototype)中有 0 个。我不明白可能出了什么问题。

var iniList = [
new Spot("Park"),
new Spot("High School"),
new Spot("Soccer Stadium"),
new Spot("Railway Station"),
new Spot("Hospital")
];


var viewModel = {
spots: ko.observableArray(iniList),
filter: ko.observable(''),
search: function(value) {
console.log(iniList);

viewModel.spots.removeAll();
for (x = 0; x < iniList.length; x++) {
console.log("iniList[x]");
if (iniList[x].name.toLowerCase().indexOf(value.toLowerCase()) >= 0) {
viewModel.spots.push(iniList[x]);
}
}
}
};

最佳答案

您正在运行“viewModel.spots.removeAll();”这会删除可观察数组的所有元素。

关于Javascript Array 长度为 0 的非空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41928541/

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