Один из способов, которым вы могли бы go сделать это, - объединить index
и city
для каждого объекта и создать уникальный идентификатор.
const data = [
[
{ id: 1, city: "New York", patients: 100 },
{ id: 2, city: "Boston", patients: 50 },
],
[
{ id: 1, city: "New York", patients: 120 },
{ id: 2, city: "Boston", patients: 75 },
]
];
function App() {
return (
<div>
{data.map((items, idx) => (
<ul key={idx}>
{items.map((item) => (
<li key={`${idx}-${item.id}`}>
[{idx}-{item.id}] {item.city}: {item.patients}
</li>
))}
</ul>
))}
</div>
);
}
ReactDOM.render(
<App />,
document.getElementById("app")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>
Или, если вы хотите просто сгладить массив и поместить все объекты в один массив, вы можете сделать нечто подобное, как я делал выше:
const data = [
[
{ id: 1, city: "New York", patients: 100 },
{ id: 2, city: "Boston", patients: 50 },
],
[
{ id: 1, city: "New York", patients: 120 },
{ id: 2, city: "Boston", patients: 75 },
]
];
const flattened = data.reduce(
(accumulator, items, idx) => {
const cities = items.map((city) => {
return {
uniqId: `${idx}-${city.id}`,
...city
};
})
return [
...accumulator,
...cities
];
},
[]
)
console.log(flattened)