As a react apprentice, I'm having a hard time mapping data
into a new object grouping by it states, I´ve tried example_1 and example_2 with no success, also saw a lot of simple mapping
questions that doesn't apply into my case.
My App.js:
import "./styles.css";
export default function App() {
const data = [
{
"id": 1,
"name": "Chris",
"state": "MA",
"stateId": 1
},
{
"id": 2,
"name": "Jenna",
"state": "MA",
"stateId": 1
},
{
"id": 3,
"name": "Pat",
"state": "RI",
"stateId": 2
},
{
"id": 4,
"name": "Dan",
"state": "RI",
"stateId": 2
}
];
const newdata = data.map((comp) => ({
stateId: comp.stateId,
state: comp.state,
person: [{ id: comp.id, name: comp.name }]
}));
console.log(newdata)
return (
<div className="App">
<h2>
<h1>test</h1>
</h2>
</div>
);
}
I would like to have something like this:
[
{
"stateId": 1,
"state": "MA",
"person": [
{
"id": "1",
"name": "chris"
},
{
"id": "2",
"name": "Jenna"
}
]
},
{
"stateId": 2,
"state": "RI",
"person": [
{
"id": "3",
"name": "Pat"
},
{
"id": "4",
"name": "Dan"
}
]
}
]