Reputation: 771
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?
Upvotes: 0
Views: 1599
Reputation: 4014
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');
Upvotes: 1
Reputation: 50807
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
}
Upvotes: 0
Reputation: 120
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
Upvotes: 0
Reputation: 4277
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;
Upvotes: 0