My goal is to implement a filtering function on my input that will display a different result.
However, I have encountered an issue where the this.data.stationInfo.stations
array is being changed, as shown in the console.log output. According to the Mozilla docs, the filter should create a new array instead of modifying the existing one. I suspect that the problem lies with how I am using this
. Does anyone have any ideas on how to resolve this?
onTextChanged: function () {
let copy = this.data.stationInfo.stations;
console.log("this.data.stationInfo.stations.length", this.data.stationInfo.stations.length);
copy = copy
.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 = copy;
console.log("copy.length", copy.length);
console.log("after copy.length this.data.stationInfo.stations.length", this.data.stationInfo.stations.length);
},
console.log output:
JS: 'this.data.stationInfo.stations.length' 239
JS: 'copy.length' 4
JS: 'after copy.length this.data.stationInfo.stations.length' 4
JS: 'this.data.stationInfo.stations.length' 4
JS: 'copy.length' 0
JS: 'after copy.length this.data.stationInfo.stations.length' 0
JS: 'this.data.stationInfo.stations.length' 0
JS: 'copy.length' 0
JS: 'after copy.length this.data.stationInfo.stations.length' 0
JS: 'this.data.stationInfo.stations.length' 0
JS: 'copy.length' 0
JS: 'after copy.length this.data.stationInfo.stations.length' 0
UPDATE:
Check out the NativeScript PlayGround example here: