javascript - 试图过滤数组
问题描述
我有这个代码:
console.log(this.productlist[this.productlist.length-1] === product); //says: true
this.productlist == this.productlist.filter(p => p !== product);
console.log(this.productlist); //the product is still in the list
我想product
从this.productlist
. 第一个 console.log 说的是真的,所以两个对象是一样的。但是第二个 console.log 显示该产品仍在列表中。
我在其他地方使用过滤器,它在那里工作。我一无所知。我该怎么做才能找出为什么这在这里不起作用?
解决方案
从数组中删除最后一项的各种方法:
使用过滤器:
array = array.filter((elem,index) => index != array.length-1)
使用拼接:
array.splice(array.length-1)
使用切片:
array.slice(0,array.length-1)
推荐阅读
- css - 如何在键盘可访问性焦点上关闭汉堡菜单
- r - prcomp R 基函数中的主动变量和补充变量
- python - 给定像素 (x,y) 位置返回 (row, column) 元组
- php - 如何使用 PHP 更新现有的数组数组?
- javascript - Javascript 生成器不会产生新的结果
- c# - 天蓝色函数时间触发器和天蓝色sql数据库与c#之间的连接:错误。如何修复它
- scala - 如何将查询写入 es.query
- html - 如何通过 pathVariable 在 url 中使用我的 HTML 表单的字段?
- java - Android自定义视图onDraw方法在第二次启动活动无效后未调用
- swift - swift MaterialComponents.MDCTabBarView mdc_customView 无法正常工作