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:
let bod = req.body.books; const filtered = bod.map(function (rep){ console.log(rep); return rep.bookStatus; });
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:
if(rep.bookStatus === 'published') { return ({ bookStatus: rep.bookStatus }) }
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
let bod = req.body.books; const filtered = bod.map(function (rep){ console.log(rep); return { bookStatus : rep.bookStatus }; });