I am implementing a restful api to do stuff just with a local file:
data.js:
let store = {
posts: [
{
id: 1,
name: 'Top 10 ES6 Features every Web Developer must know',
url: 'https://webapplog.com/es6',
text: "This essay will give you a quick introduction to ES6. If you don’t know what is ES6, it’s a new JavaScript implementation.",
comments: [
{ text: 'Cruel…..var { house, mouse} = No type optimization at all' },
{ text: 'I think you’re undervaluing the benefit of ‘let’ and ‘const’.' },
{ text: '(p1,p2)=>{ … } ,i understand this ,thank you !' }
]
},
{
id: 2,
name: 'anotherPost',
url: 'https://webapplog.com/es6',
text: "This essay will give you a quick introduction to ES6. If you don’t know what is ES6, it’s a new JavaScript implementation.",
comments: [
{ text: 'Cruel…..var { house, mouse} = No type optimization at all' },
{ text: 'I think you’re undervaluing the benefit of ‘let’ and ‘const’.' },
{ text: '(p1,p2)=>{ … } ,i understand this ,thank you !' }
]
}
]
}
module.exports = store;
For example here´s how I do a Post request to create another post
:
router.post('/', (req, res) => {
data.posts.push({
id: req.body.id,
name: req.body.name,
url: req.body.url,
text: req.body.text,
comments: [
req.body.comments
]
})
res.send(data.posts)
})
Or here´s how I delete a post (i actually add it the id property in order to do this, although minutes later i found out it wasn´t neccesary, but because of it it wasn´t the reason it came up the creation of this question)
router.delete('/:postId', (req, res) => {
const post_id = req.body.id;
const index = post_id -1;
data.posts.splice(index, 1);
res.send(data.posts)
})
So when I try to do the put route i came up with this, although later i also found out i could just use data.posts[index].name = etc...
but I decided to open this question because i have really curiosity in how something can this could work (obviously something similar since the following code does not work):
data.posts.filter(post => {
post.id === req.params.postId;
}).then(post => {
post.id = req.body.id,
post.name = req.body.name,
post.url = req.body.url,
post.text = req.body.text,
post.comments = [
req.body.comments
]
})
What am trying to do there is once the correct post has been filtered, then modify the properties of that post. I´ve been doing javascript for months but i have always been blindly following tutorials and never stop to actually learn how callbacks work or how that code is not possible. But because i see similar code to work (callbacks in express), i was wondering if somebody could give some direction.
As i said i already have the simple solution sorted it out but i am very curious in how i could so something like that with the filter function (or just educate me in how this things works)
Advertisement
Answer
Since the Array#filter
method is synchronous and returns the filtered array, you can chain the Array#map
function to it in order to transform the elements of the filtered array. There’s no need for a “callback” or promise in the sense that the code is all synchronous … for iterative methods like map and filter, the function argument is typically called an “iteratee”.
So for your last code block, you can simply do something like this:
const filteredAndModifiedPosts = data.posts.filter(post => {
return post.id === req.params.postId;
}).map(post => {
post.id = req.body.id,
post.name = req.body.name,
post.url = req.body.url,
post.text = req.body.text,
post.comments = [
req.body.comments
]
return post
})