-2
let arr = [{
question:{
 q1 : 123,
 q2 : 44
}
}];

I've tried using forEach loop to do it, but it returns nothing.

Arnold
  • 7
  • 1
  • There's only one element in `arr` so why `.forEach()`? `.forEach()` doesn't "return" anything -> [How do I ask a good question? - Help Center - Stack Overflow](https://stackoverflow.com/help/how-to-ask) – Andreas Jun 28 '20 at 15:38
  • post your code so others can point out your mistake – Yousaf Jun 28 '20 at 15:46
  • That's a small part of it, but I've fixed the problem. but you can still say something – Arnold Jun 28 '20 at 18:43

1 Answers1

1

If it's an array then you can do either map or forEach.

  arr.forEach()

However you have to pay attention to the internal structure of the item as well, ex. in your case

  arr.forEach(item => {
    console.log(item.question.q1)
  })

And if you want the transformation, then

  const transformedArr = arr.map(item => item.question.q1)
windmaomao
  • 7,120
  • 2
  • 32
  • 36