I have a set of incoming data (I've simplified the objects for easier understanding). This data could have hundreds or even thousands of properties.
teams = {
teams: ['Lakers', 'Clippers', 'Bucks', 'Suns'],
states: ['California', 'California', 'Wisconsin', 'Arizona']
}
It's important to note that each property will always contain the same number of items.
My goal is to transform this data into an Array List of objects:
teamsList = [
{ teams: 'Lakers', states: 'California' },
{ teams: 'Clippers', states: 'California' },
{ teams: 'Bucks', states: 'Wisconsin' },
{ teams: 'Suns', states: 'Arizona' }
]
This is my initial approach and attempt:
const parseData = (obj) => {
const teams = obj.teams;
const states = obj.states;
let newList = [];
teams.forEach((item, index) => {
const teamData = {
teams: item,
states: states[index]
}
newList.push(teamData);
})
return newList;
}
I am curious if there is a more efficient way to accomplish this task?
I forgot to mention that I'm looking for a method where the keys in the new array list can be dynamic too - avoiding having to specifically write out each property:
teams.forEach((item, index) => {
const teamData = {
teams: item,
states: states[index],
n: n[index]
}
newList.push(teamData);
})
In this case, n represents any possible object key.
Thank you.