Trying to find out the quickest way to update an object's value in the array.
- You wouldn't know the exact position, hence doing
animes[0]readStatus=trueis not possible - I'm trying to avoid for loop as the array may be A LOT. Example 1 million. Though it seems that for loop is faster, tested by someone and posted here in Medium
Following is my solution to tackle this, could you suggest something better?
const animes = [
{
id:'1',
title:'latest anime',
readStatus:false,
imageSource:'https://help.twitter.com/content/dam/help-twitter/twitter-logo.png'
},
{
id:'2',
title:'naruto',
readStatus:true,
imageSource:'https://mapstr-prod.s3.amazonaws.com/298ed6c57ada2a7560d24dabba452548_facebook-logo-F-1200x816.jpg'
},
{
id:'3',
title:'one piece',
readStatus:true,
imageSource:'https://mapstr-prod.s3.amazonaws.com/298ed6c57ada2a7560d24dabba452548_facebook-logo-F-1200x816.jpg'
},
{
id:'4',
title:'you name it',
readStatus:true,
imageSource:'https://mapstr-prod.s3.amazonaws.com/298ed6c57ada2a7560d24dabba452548_facebook-logo-F-1200x816.jpg'
}
]
let rebels = animes.filter((anime) => {
return anime.id === '1';
});
let newItems = animes.filter((anime) => {
return anime.id !== '1';
});
let myMap = Object.assign({}, rebels)
myMap[0].readStatus = true;
newItems.push(myMap[0])
console.log(newItems);
idof the nested objects, would allow you to do quick look ups from that point on.