在过滤后的数组中更改布尔值

时间:2018-11-12 18:38:54

标签: javascript

我有数组,我需要编写一个接受用户名的函数,并且布尔值应更改为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();

我认为我应该过滤数组,然后将bool改为相反,但是在那之后map函数仅返回bool。如何更改布尔值并将更改推送到原始数组?

4 个答案:

答案 0 :(得分:1)

首先,您需要使用所需的值过滤该数组,然后得到一个过滤后的数组,现在您映射到该数组以更新布尔值。

// 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');

答案 1 :(得分:0)

这行代码有效,但只返回过滤/映射的值

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

在您的函数中返回数组

array.filter(item => item.user === user).map(item => item.read = true)
return array;

答案 2 :(得分:0)

您可以像这样在map函数中将read设置为true:

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

[]的

答案 3 :(得分:0)

此版本不会修改您的消息,而只会返回经过过滤的更新列表:

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)

此版本返回新的邮件完整列表,匹配的邮件已更改:

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)

此版本将更新到位的项目列表。 (除非绝对必要,否则我建议您这样做。不可变数据具有很大的好处!)

const updateMessages = (items, userToCheck) => {
  items.forEach(item => {
    if (item.user == userToCheck) {
      item.read = !item.read
    }
  })
  return items
}