-4

Im quite new to front-end development. I am using React.

My problem: How to extract object value that are within array?

   myArr = [[{a: 1, b: 2}], [{a: 1, b:2}], [{a: 1, b:2}]]
   // I want to extract only the values of b and make them into array 
   // my asnwer should look like this: 
   // [2,2,2]

My Approach:

  const [answerArr, setAnswerArr] = useState([]);
  useEffect(() => {
    const extractCode = () => {
      const res = myArr.map((item)=>{
          ????
       })
    };
    extractCode();
  }, [codeArr]);

I've tried using map method...but I am struggling very much... If you could help me i would learn so much!

jabaa
  • 5,844
  • 3
  • 9
  • 30
jaehyuk kim
  • 121
  • 7

1 Answers1

3

Your code should look something like this

 const res = myArr.map(item => item[0].b)
MrFabio_25
  • 478
  • 5
  • 9