Reputation: 973
When I select the item from the list, and select save, the item should be removed from the list. I'm able to retrieve the selected item from using this method - form.value.selectedMessages - which gives me an array of the object I selected. The overall number of received messages which is an array is rMessages. What's happening here is both of my messages are being removed and not just the selected one. It's probably something easy that I'm missing. Appreciate it!
save(form){
this.rMessages = this.rMessages.filter(resultTwo => {
form.value.selectedMessages.map(resultOne => {
return resultTwo.messageID !== resultOne.messageID
})
})
}
Data
List of messages --
[{messageID: 1, message: "message One"},
{messageID: 2, message: "message Two"}]
Selected Message or could be both
[{messageID: 1, message: "message One"}]
if One is selected then below should only show
[{messageID: 2, message: "message Two"}]
Upvotes: 0
Views: 56
Reputation: 4021
Well to start you need to return something in your filter function;
In your code there is no return. This means that it will default to returning undefined
which is basically false
. Because it's always returning false everything is filtered from the array.
The logic also looks a bit dodgy.
map
will return a new array, which i don't think that is what you want. Instead you could use find
, some
, or every
based on your situation.
Here is a reworked version of your code with find
this.rMessages = this.rMessages.filter(resultTwo => {
return !form.value.selectedMessages.find(resultOne => {
return resultTwo.messageID === resultOne.messageID
})
})
Below is a working, modified example but the idea is still the same
var rMessages = [1,2,3,4,5].map(item => {return {messageID:item}});
var selected = [2,5].map(item => {return {messageID:item}});
var form = {
value:{
selectedMessages:selected
}
};
rMessages = rMessages.filter((resultTwo) => {
return !form.value.selectedMessages.find((resultOne) => {
return resultTwo.messageID === resultOne.messageID
})
})
console.log(rMessages)
Upvotes: 1