Austin Johnson
Austin Johnson

Reputation: 747

Combine values from two different object keys into one object key value

I have an array of objects coming from an api that looks like this

[{"HomeTeam": "HOU"}, {"AwayTeam": "GB"}, {"HomeTeam": "DAL"}, {"AwayTeam": "TB"}]

I'm using map to map over the objects and get the team names to put them into react table like this.

const results = data.map((teams) => ({
    "Teams": // I need both teams here
}))

How can I map over the array and get both HomeTeam and AwayTeam values into Teams values?

I need the final object to look like this

[{"Teams": "HOU"}, {"Teams": "GB"}, {"Teams": "DAL"}, {"Teams": "TB"}]
 

Upvotes: 2

Views: 961

Answers (1)

Emilio Grisolía
Emilio Grisolía

Reputation: 1203

If the objects only have one key, this should be fine.

const data = [{"HomeTeam": "HOU"}, {"AwayTeam": "GB"}, {"HomeTeam": "DAL"}, {"AwayTeam": "TB"}];

const results = data.map(td => ({ Teams: Object.values(td)[0] }));

console.log(results);

Upvotes: 3

Related Questions