我想过滤输入,然后显示不同的结果。
但是就像您在console.log输出中看到的那样,它更改了我的r <- list(First = c("Jan", "feb", "March", "Ram"), Second = c("Jan",
"feb", "April"), Third = c("Jan", "feb", "May", "Shyam"), Fourth = c("Jan",
"feb", "June", "abcd", "July"), Fifth = c("Jan", "feb", "asdfgg",
"dfhfhsa", "qwer"))
数组。
过滤器应创建一个新数组,而不更改我从mozilla docs中读取的数组。我认为问题在于this.data.stationInfo.stations
的用法?有人知道如何解决吗?
this
console.log输出:
JS:“ this.data.stationInfo.stations.length” 239
JS:“ copy.length” 4
JS:“在copy.length this.data.stationInfo.stations.length之后” 4
JS:'this.data.stationInfo.stations.length'4
JS:“ copy.length” 0
JS:“在copy.length this.data.stationInfo.stations.length之后” 0
JS:'this.data.stationInfo.stations.length'0
JS:“ copy.length” 0
JS:“在copy.length this.data.stationInfo.stations.length之后” 0
JS:'this.data.stationInfo.stations.length'0
JS:“ copy.length” 0
JS:“在copy.length this.data.stationInfo.stations.length之后” 0
更新:
NativeScript PlayGround:https://play.nativescript.org/?template=play-vue&id=1SXSNW
答案 0 :(得分:1)
您不应创建引用。实际上,您设置了对原始数组的引用,然后针对您复制的所有突变都会反映在原始数组上。
onTextChanged: function () {
console.log("this.data.stationInfo.stations.length", this.data.stationInfo.stations.length);
let newArray = this.data.stationInfo.stations
.filter(el => {
return el.eng站名.toLowerCase().startsWith(this.data.searchBar.search.toLowerCase())
||
el.站名.toLowerCase().startsWith(this.data.searchBar.search.toLowerCase())
||
el.traWebsiteCode.startsWith(this.data.searchBar.search.toLowerCase())
});
this.data.resultDetails.stations = newArray;
console.log("copy.length", copy.length);
console.log("after copy.length this.data.stationInfo.stations.length", this.data.stationInfo.stations.length);
}
我也建议您改善代码并在一处完成所有需要的工作。您可以遍历数组并直接排除所有需要的工作站。这里是一个例子:
onTextChanged: function () {
const stations = this.data.stationInfo.stations;
function doSearch(el,str) {
return
el.eng站名.toLowerCase().startsWith(str) ||
el.站名.toLowerCase().startsWith(str) ||
el.traWebsiteCode.startsWith(str);
}
for(let j=0; j < stations.length; j++){
if(!doSearch(stations[j], this.data.searchBar.search.toLowerCase())){
stations.splice(j, 1);
}
}
console.log(stations);
}