I am trying to loop through the req.body
that has data and I am trying to return the bookStatus
of all the books present inside the body. I am doing this:
JavaScript
x
6
1
let bod = req.body.books;
2
const filtered = bod.map(function (rep){
3
console.log(rep);
4
return rep.bookStatus;
5
});
6
This returns the bookStatus
of all the books, but it is just the value of the bookStatus
key. I am trying to get the key as well, for it to look like {bookStatus:"value"}
.
UPDATE: solution posted by @evolutionxbox works, except now I am trying to access only the ones that have the status published. I am doing this:
JavaScript
1
4
1
if(rep.bookStatus === 'published') {
2
return ({ bookStatus: rep.bookStatus })
3
}
4
But this returns the results as following [ { bookStatus: 'published' }, { bookStatus: 'published' }, undefined ]
. Here undefined
is the unpublished
one but I do not even want it to be there in first place
Advertisement
Answer
JavaScript
1
6
1
let bod = req.body.books;
2
const filtered = bod.map(function (rep){
3
console.log(rep);
4
return { bookStatus : rep.bookStatus };
5
});
6