0

I have array and i need to write a function that accepts the name of the user and and boolean should be changed to true.

var array = [{
  user: "Sasha",
  message: "Hello guys",
  time: "20:28:2",
  read: false
}, {
  user: "Sasha",
  message: "How are you doing",
  time: "20:28:2",
  read: false
}, {
  user: "Dima",
  message: "I am fine, thanks!",
  time: "20:28:2",
  read: false
}, {
  user: "Katya",
  message: "I am doing well! What about you?",
  time: "20:28:2",
  read: false
}]

function readMessage(user) {
  let test = array
  let filtered = test.filter(item => item.user === user);
  let y = filtered.map(item => item.user && !item.read);
  console.log(y);
}

readMessage();

I think i should filter array, and then change bool to the opposite, but after that map function returns only bools. How to change boolean and push changes to the original array?

Towkir
  • 3,889
  • 2
  • 22
  • 41
Sasha Zoria
  • 739
  • 1
  • 9
  • 28
  • Possible duplicate of [Modify object property in an array of objects](https://stackoverflow.com/questions/16691833/modify-object-property-in-an-array-of-objects) – Heretic Monkey Nov 12 '18 at 18:40
  • `.map` and `.filter` create copies of the original array, you need to use `for` or `.forEach` and operate on `user` directly if you want to modify the original array – Rob M. Nov 12 '18 at 18:46

4 Answers4

1

First you need to filter the array with your desired value, then you get a filtered array, now you map over that array to update the boolean value.

// put your messages array in a variable
var array = [{
  user: "Sasha",
  message: "Hello guys",
  time: "20:28:2",
  read: false
}, {
  user: "Sasha",
  message: "How are you doing",
  time: "20:28:2",
  read: false
}, {
  user: "Dima",
  message: "I am fine, thanks!",
  time: "20:28:2",
  read: false
}, {
  user: "Katya",
  message: "I am doing well! What about you?",
  time: "20:28:2",
  read: false
}]

function readMessage(user) {
  let test = array
  let filtered = test.filter(item => item.user === user);
  // console.log(filtered); // check the filtered array;
  filtered.map(item => item.read = true);
  console.log(filtered);
}
// call the function now;
readMessage('Sasha');
Towkir
  • 3,889
  • 2
  • 22
  • 41
0

This line of code worked but just returns your filtered/mapped values:

array.filter(item => item.user === 'Sasha').map(item => item.read = true)

In your function return the array instead

array.filter(item => item.user === user).map(item => item.read = true)
return array;
Jonathan Stellwag
  • 3,843
  • 4
  • 25
  • 50
0

You can just set read to true inside the map function like this:

function readMessage(user){
   let test = array
   let filtered = test.filter(item => item.user === user);
   let y = filtered.map(item => item.read = true);
}

[]'s

0

This version does not modify your messages in place but only returns the filtered, updated list:

const items = [
  {user: "Sasha", message: "Hello guys", time: "20:28:2", read: false},
  {user: "Sasha", message: "How are you doing", time: "20:28:2", read: false},
  {user: "Dima", message: "I am fine, thanks!", time: "20:28:2", read: false},
  {user: "Katya", message: "I am doing well! What about you?", time: "20:28:2", read: false}
] 

const readMessages = (items, user) => items
  .filter(item => item.user === user)
  .map(({read, ...rest}) => ({...rest, read: !read}))

const updatedItems = readMessages(items, 'Sasha')

console.log(updatedItems)

This version returns a new entire list of messages, with the matching ones changed:

const items = [
  {user: "Sasha", message: "Hello guys", time: "20:28:2", read: false},
  {user: "Sasha", message: "How are you doing", time: "20:28:2", read: false},
  {user: "Dima", message: "I am fine, thanks!", time: "20:28:2", read: false},
  {user: "Katya", message: "I am doing well! What about you?", time: "20:28:2", read: false}
] 

const updatedMessages = (items, userToCheck) => items
  .map(({user, read, ...rest}) => user == userToCheck
      ? {...rest, user, read: !read}
      : {...rest, user, read}
  )

const updatedItems = updatedMessages(items, 'Sasha')

console.log(updatedItems)

And this version updates the list of items in place. (I would recommend against this unless it's absolutely necessary. Immutable data has great benefits!)

const updateMessages = (items, userToCheck) => {
  items.forEach(item => {
    if (item.user == userToCheck) {
      item.read = !item.read
    }
  })
  return items
}
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103