Javascript 正在尝试筛选数组
我有以下代码:Javascript 正在尝试筛选数组,javascript,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 我想从这个.productlist中筛选出产品
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
我想从这个.productlist
中筛选出产品(它是列表中的最后一项)。第一个console.log为true,因此这两个对象是相同的。但是第二个console.log显示该产品仍在列表中
我在其他地方使用过滤器,在那里它可以工作。我不懂。我能做些什么来找出为什么这里不起作用?从数组中删除最后一项的各种方法:
使用过滤器:
array=array.filter((elem,index)=>index!=array.length-1)
使用接头:
array.splice(array.length-1)
使用切片:
array.slice(0,array.length-1)
从数组中删除最后一项的各种方法:
使用过滤器:
array=array.filter((elem,index)=>index!=array.length-1)
使用接头:
array.splice(array.length-1)
使用切片:
array.slice(0,array.length-1)
如果您特别关注最后一个元素,则可以使用pop
设a=[1,2,3,4]
设b=a.pop()
控制台日志(a)
console.log(b)
如果您特别关注最后一个元素,则可以使用pop
设a=[1,2,3,4]
设b=a.pop()
控制台日志(a)
console.log(b)
在第2行中,您使用的是=
相等运算符,而不是赋值=
在第2行中,您使用的是=
相等运算符,而不是赋值=
您使用的是=
相等运算符,而不是赋值
。您应该将此作为答案发布, @SachinGupta@SachinGupta哦,非常感谢你!我看了那么长时间的代码,但我只是没有看到double=
…@nbar也将其作为答案发布。您使用的是=
相等运算符,而不是赋值=
。您应该将其作为答案发布@SachinGupta@SachinGupta哦,非常感谢你!我看了这么长时间的代码,但我只是没有看到双=
…@nbar也把它作为一个答案发布了出来。