Push array object values into new arrays

Viewed 34

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

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

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