0

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"}]

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

4 Comments

This answer doesn't work unfortunately, nothing happens :( When I log this.rMessages after the function, nothing is coming back.
@userlkjsflkdsvm Sorry there was a bug, I adjusted the answer for you and gave a working example.
@userlkjsflkdsvm The additional code you provided in your question will not aid in debugging this. I would check that the values you think you are passing into your function are correct. It seems that the bug not related to this function any more, and possibly not related to the original question
Ah yes I was making another error elsewhere. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.