Reputation: 2507
I would like to filter results such that when I compare the value under Score with the value under against, I return the winning team
Using this as an example
const newData = [
{"Year": 1,
"Score": 3,
"Against": 5,
"GameID": 2,
"Team": 'A' },
{"Year": 1,
"Score": 5,
"Against": 3,
"GameID": 2,
"Team": 'B' }
]
My desired output would be ['B']
This is what I have tried
const newArr = newData.filter(element => element.Score > element.Against)
console.log(newArr.Team)
This returns undefined
Upvotes: 1
Views: 204
Reputation: 350127
newArr
is an array, and the property Team
does not relate to it, but to its elements. You can use map
to convert the array of objects to an array of team name(s):
const newData = [{"Year": 1,"Score": 3,"Against": 5,"GameID": 2,"Team": 'A'},{"Year": 1,"Score": 5,"Against": 3,"GameID": 2,"Team": 'B'}]
const newArr = newData.filter(element => element.Score > element.Against)
.map(element => element.Team);
console.log(newArr)
Upvotes: 1
Reputation: 7651
You are getting this as resault:
You can do it by accessing newArr[0].Team
const newData = [
{"Year": 1,
"Score": 3,
"Against": 5,
"GameID": 2,
"Team": 'A' },
{"Year": 1,
"Score": 5,
"Against": 3,
"GameID": 2,
"Team": 'B' }
]
const newArr = newData.filter(element => element.Score > element.Against)
console.log(newArr[0].Team)
And it will print B
Upvotes: 0