I'd like to refactor a block of code which basically has to map 2 levels to the data I want. The data I want is the value of body in a object that contains ['key'] that has array of objects .
My object looks as follows,
const object = {
"array-one": [
{
id: 1,
body: "need_help",
},
{
id: 2,
body: "no_help",
},
],
}
and below is how I map to get the value of body
const getTheBodyValue = () => Object.keys(object).map(key => {
object[key].map((elem) => {
const { id, body } = elem;
if (body === 'need_help') // do something
if (body === 'no_help') // do something
});
});
is there a more simpler way to achieve the value of body in "array-one" & "array-two"?
mapwithout using the result? what are you doing inside the clauses?mapto loop through things. UseforEachor aforloop. Usemapto create a new array.