I have declared this empty map mapA = {}
but for some reason when I set it to a parsed json mapA = parsedJSON
would result in an error of same or duplicate keys.
The json which I fetched is something like this [{a:1},{a:2},{a:3}]
, just to clarify...this is a json right? Cause its also kind of an array right?
But I fetched this as a json string and then parse it and then I receive the error I mentioned.
However, mapA = [{a:1},{a:2},{a:3}]
works for me but this is hard coded. What part is wrong in here actually?
EDIT1:
Here is a sample data which I used
[{"location_id":1,"flight_location":"KUL","description":"Kuala Lumpur","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":2,"flight_location":"KIX","description":"Osaka-Kansai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":3,"flight_location":"PEK","description":"Beijing","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":4,"flight_location":"HGH","description":"Hangzhou","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":5,"flight_location":"PVG","description":"Shanghai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":6,"flight_location":"SZX","description":"Shenzhen","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":7,"flight_location":"DPS","description":"Bali","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":8,"flight_location":"CGK","description":"Jakarta","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":9,"flight_location":"DMK","description":"Bangkok","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0}]
Here is my code:
let fakeData = [{"location_id":1,"flight_location":"KUL","description":"Kuala Lumpur","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":2,"flight_location":"KIX","description":"Osaka-Kansai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":3,"flight_location":"PEK","description":"Beijing","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":4,"flight_location":"HGH","description":"Hangzhou","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":5,"flight_location":"PVG","description":"Shanghai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":6,"flight_location":"SZX","description":"Shenzhen","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":7,"flight_location":"DPS","description":"Bali","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":8,"flight_location":"CGK","description":"Jakarta","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":9,"flight_location":"DMK","description":"Bangkok","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0}];
let flights = {};
fetch(link) //link is the api link which I use to fetch the data
.then(function(response) {
return response.json(); //In here I parse it
}).then(function(json) {
/*flights = fakeData; Causes same error. Run this only to test when api is down */
flights = json; // Same error. Set our flights map to the parsed json which is the map
}).catch(function(ex) {
console.log('parsing failed', ex);
});
Console output:
This is from the React library. Btw , I'm not really familiar with the codes in their library.
My components:
FlightList
const FlightList = ({flights, deleteFlight}) => {
return (
<table className="table">
<thead>
<tr>
<th>ID</th>
<th>Location</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{flights.map(flight =>
<FlightListRow key={flight.location_id} flight={flight}/>
)}
</tbody>
</table>
);
};
FlightListRow
const FlightListRow = ({flight}) => {
return (
<tr>
<td><Link to={'/editflight/' + flight.location_id}>{flight.location_id}</Link></td>
<td><Link to={'/editflight/' + flight.location_id}>{flight.flight_location}</Link></td>
<td><Link to={'/editflight/' + flight.location_id}>{flight.description}</Link></td>
</tr>
);
};