How can I transform a string API response into an array of objects?
When making a GET request, the result is returned as a single long string:
const state = {
strict: true,
airports: []
};
const getters = {
allAirports: (state) => state.airports,
};
const actions = {
getAirports({ commit }) {
axios.get('https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat')
.then(response => {
commit('SET_AIRPORTS', response.data)
})
}
};
const mutations = {
SET_AIRPORTS(state, airports) {
state.airports = airports
}
};
The response looks like this:
1,"Goroka Airport","Goroka","Papua New Guinea","GKA","AYGA",-6.081689834590001,145.391998291,5282,10,"U","Pacific/Port_Moresby","airport","OurAirports" ...
Instead, I want to split this string into separate objects and append them to the airports
array like this:
airports: [
{
id: 1,
name: 'Goroka Airport',
city: 'Goroka',
country: 'Papua New Guinea',
iata: 'GKA',
icao: 'AYGA',
longitude: -6.081689834590001,
latitude: 145.391998291
},
{
etc...
}
]
Can you provide guidance on how to achieve this transformation?