javascript - 如何从 JavaScript 数组中删除元素?
问题描述
我想删除数组中的值,但下面的代码不起作用:
stations=['stations1','stations2']
wayPoints=[{location: "stations1"},{location: "stations2"},{location: "stations3"},{location: "stations4"}]
deletStations(){
let result=[];
this.stations.forEach(index1 => rest.push(this.arrayRemove(this.wayPoints,index1)) );
}
arrayRemove(arr, value) {
return arr.filter(function(ele){
return ele != value;
});
}
上面的这段代码不会{location: "stations1"}, {location: "stations2"}
从航路点中删除,请问有什么建议吗?
解决方案
这是一种方法:
const secondWayOfDoingIt = wayPoints.filter(
element => !stations.includes(element.location)
);
同样的,但有解构的论点:
const firstWayOfDoingIt = wayPoints.filter(
({ location }) => !stations.includes(location)
);
希望能帮助到你。
推荐阅读
- java - 在执行文件操作时在 java 应用程序中打开太多文件描述符
- excel - 识别所有已被条件格式化的单元格
- antlr4 - 将 ANTLR 解析树转换为字符串,然后将其还原
- php - 如何将另一种语言日期格式转换为英语语言格式 (Ymd)?
- windows - 如何在 Lua 中创建具有 Windows-1251 编码的文本文件?
- angular - 日期选择器和时间选择器验证
- java - 在android studio中查找下周四的日期
- java - 如何使 OkHttp 调用同步/阻塞?
- python - 为什么数字和数据框标题之间有零?
- javascript - for循环中的setTimeout不返回预期结果