0

const aboutMe = [{
   "name": "frank",
   "about": [{
       "mood": "happy",
       "dinner": [{
         "first": "desert",
         "last": "noodles"
       }]
     },
     {
       "mood": "happy",
       "dinner": [{
         "first": "desert",
         "last": "noodles"
       }]
     },
     {
       "mood": "happy",
       "dinner": []
     }
   ]
}]

const AllBreak = aboutMe.about.map((dinner) => ((dinner.first, dinner.last)));


   const expectedOutput =["first": "desert", "last": "noodles", "first": "desert", "last": "noodles"]
console.log(aboutMe, AllBreak, expectedOutput)

so am trying to filter through a nested array learning from a tutorial I don't know why it returns cannot read property of map why is that pretty sure i filtered correctly according to the tutorial

2
  • 1
    aboutMe is an array, if you are interested only at the first element, you can so something like: aboutMe[0].about but i doubt about it Commented May 7, 2021 at 17:53
  • 1
    What is the expected output? Does aboutMe array always have a single object in it? Commented May 7, 2021 at 17:53

2 Answers 2

2
  • Firstly, aboutMe is an array with an object that has an about property in it. So, if you want to access this property, you need to first access the first element of the array and then access the about property in it.

  • Secondly, (dinner.first, dinner.second) doesn't actually make any sense here. Because when you have multiple expressions separated by commas in a bracket, each of those expressions get evaluated but only the last one is returned. So, here returning (dinner.first, dinner.second) is equivalent to returning dinner.second.

  • So, if you only want dinner.second then just return that or put them in an array (or object) and return that.

  • Also, since in your example it seems that it is not guaranteed that the dinner array would always have an object inside it, it is best to use Optional Chaining here.

Please have look at the solution below:

const 
  aboutMe = [{name:"frank",about:[{mood:"happy",dinner:[{first:"desert",last:"noodles"}]},{mood:"happy",dinner:[{first:"desert",last:"noodles"}]},{mood:"happy",dinner:[]}]}],
  res = aboutMe[0].about.map(({dinner}) => [dinner?.[0]?.first, dinner?.[0]?.last])
  
console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

1

aboutMe is an array, if you want to get the property of the first element, you can use indexing [0]

const AllBreak = aboutMe[0].about.map(() => ...);

3 Comments

1) (dinner.first, dinner.last) is same as dinner.last. 2) objects inside about array don't have a first property. They have a dinner property which in turn has objects with first as property
It is wrong indeed, I was only trying to point to the fact that aboutMe is an array and he should point to a specific item. I didn't look to the further functionality excuse me I will set this right.
I did not, I would never do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.