I have an array of posts where I would like to use the values from the url or route to determine what I want to show in the array, something like a filter. For example, my selected post is 'www.mywebsite.com/chocolate' so in my route where I display my posts, I only display 'chocolate' and not all the other posts. Is there something I can do like this, where the result === 'chocolate':
const post = response.data.filter(({url}) => url === 'dynamic_variable_that_knows_what_the_current_url_is_with_the_value_of_chocolate');
Below is my code for App.js:
getData() {
axios.get('/posts.json')
.then(response => {
//console.log(response);
const post = response.data;
const updatedPosts = post.map(post => {
return {
...post
}
});
this.setState({
posts: updatedPosts
});
})
.catch(error => {
console.log(error);
});
}
render() {
return (
<div className={classes.App}>
<Layout>
<Switch>
<Route path="/food/:slug" exact render={(props) => this.state.posts.map(post => { return (<FullPost key={post.id} id={post.id} pathname="/:slug" {...props} />)} )} />
<Route render={() => <h1>Whoops! What you're looking for isn't here anymore.</h1>} />
</Switch>
</Layout>
</div>
);
}