У меня есть объект, который имеет идентификаторы:
const cities = {
"0": {
"city": "Bielsko-Biała",
"country": "PL",
},
"1": {
"city": "Kielce",
"country": "PL",
},
"2": {
"city": "Kłodzko",
"country": "PL",
}
}
Я хочу удалить 0, 1, 2 и так далее, чтобы получить этот формат:
const cities = [
{
"city": "Bielsko-Biała",
"country": "PL",
},
{
"city": "Kielce",
"country": "PL",
},
{
"city": "Kłodzko",
"country": "PL",
}
]
Это необходимо для меня, потому что я хочу добавить описание к каждому городу и не могу отобразить этот объект из-за этого формата.
Мне пришлось сделать что-то подобное в React, и я думаю, что это очень плохой написанный код:
const cities = [];
countries.map(el => {
cities.push(el.city)
})
let updatedCountries = countries;
cities.forEach((city) => {
axios.get(`https://en.wikipedia.org/api/rest_v1/page/summary/${city}`)
.then(response => {
for (let property in updatedCountries) {
if (updatedCountries.hasOwnProperty(property)) {
if (updatedCountries[property].city === city) {
updatedCountries[property]['description'] = response.data.description
}
}
}
})
})
this.setState({
countries: updatedCountries
})