Reputation: 6158
I have this code:
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
I want to filter the product
(which is the last item in the list) out of this.productlist
. The first console.log says true, so both objects are the same. But the second console.log shows that the product is still in the list.
I use the filter on other places and there it works. I am clueless. What can I do to find out why this is not working here?
Upvotes: 0
Views: 64
Reputation: 5311
In line 2, you are using a ==
equality operator, not the assignment =
.
Upvotes: 1
Reputation: 138
If you are specifically after the last element you can use pop.
let a = [1,2,3,4]
let b = a.pop()
console.log(a)
console.log(b)
Upvotes: 0
Reputation: 5311
Various methods to remove last item from array:
Use filter:
array = array.filter((elem,index) => index != array.length-1)
using splice:
array.splice(array.length-1)
using slice:
array.slice(0,array.length-1)
Upvotes: 1