Home > Enterprise >  Push array object values into new arrays
Push array object values into new arrays

Time:09-16

I have data array object and two arrays scoreData and titleData as follows

scoreData=[]
titleData = []

data = [ 
{score:1, title: tesh},
 {score:2, title: teshu}, 
{score:3, title: teshiti} ]

I Wanted to push score and title values into scoreData and titleData respectively.

So the expected output will be

scoreData = [1,2,3]
titleData = [tesh,teshu,teshiti]

Thanks

CodePudding user response:

scoreData = data.map(({score}) => score);
titleData = data.map(({title}) => title);

CodePudding user response:

You can use use forEach for doing it in one loop rather than two separate loops.

data.forEach(obj => {
    scoreData.push(obj.score)
    titleData.push(obj.title)
})
  • Related