I want to display this data in table by country. First I selected all repeated countries in one array and filtered them. Next I map all data to show this in the table but I don't know how to separete this countries after mapping. My goal in this case should be:
- Germany
- Munich pop
- Berlin pop
- Dortmund pop
- England
- London pop
- Liverpool pop
- Manchester pop
... now I have country name in every row which i don't want Germany Munich pop Germany Berlin pop Germany Dortmund pop
Code:
import React from "react";
import "./styles.css";
import { data } from "./data";
export default function App() {
let countries = [];
const mapCountries = data.map((country, index) =>
countries.push(country.country)
);
countries = countries.filter(
(item, index, aref) => aref.indexOf(item) === index
);
const showCity = data.map((item, index) => (
<>
<tr>
<td colspan='2'>{item.country}</td>
</tr>
<tr>
<td>{item.city}</td>
<td>{item.pop}</td>
</tr>
</>
))
return (
<div className="App">
<table>
{showCity}
</table>
</div>
);
}
export const data = [
{
country: "Spain",
city: "Madrit",
pop: 3500000
},
{
country: "Spain",
city: "Barcelona",
pop: 1500000
},
{
country: "Spain",
city: "Valencia",
pop: 1000000
},
{
country: "Germany",
city: "Berlin",
pop: 4500000
},
{
country: "Germany",
city: "Munich",
pop: 1500000
},
{
country: "Germany",
city: "Dortmund",
pop: 1000000
},
{
country: "England",
city: "London",
pop: 8000000
},
{
country: "England",
city: "Liverpool",
pop: 500000
},
{
country: "England",
city: "Manchester",
pop: 1000000
}
];
