繁体   English   中英

如何从 JavaScript 数组中删除元素?

[英]How to remove an element from JavaScript array?

我想删除数组中的值,但我下面的代码不起作用:

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;
 });

}

上面的代码不会从 wayPoints 中删除{location: "stations1"}, {location: "stations2"} ,请问有什么建议吗?

这是一种方法:

const secondWayOfDoingIt = wayPoints.filter(
  element => !stations.includes(element.location)
);

同样,但具有解构的论点:

const firstWayOfDoingIt = wayPoints.filter(
  ({ location }) => !stations.includes(location)
);

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM