I am working on mapping JSON to Data driven rendering within react. To accomplish some of the things I need to do. I am going to require Nested JSON.
When attempting to map this data, I keep running into values that I cannot map. Specifically when trying (mapObject.object.value) where I am attempting to pass the nested values into an interpolated div.
JSON
{"PositionComponent": [
{
"key": 0,
"Elevation": {
"positionValue": "1.11566",
"id": 0
}
},
{
"key": 1,
"Azimuth": {
"positionValue": "1.145657",
"id": 1
}
}
]
}
React Components:
import React, { Component } from 'react';
import '../HeaderBar/HeaderBar.css';
import 'whatwg-fetch';
type StatusBarProps = {};
type StatusBarState = {
// tslint:disable-next-line:no-any
positionStatus: any;
};
class StatusBar extends Component<StatusBarProps, StatusBarState> {
constructor() {
super();
this.state = {
positionStatus: []
};
}
componentWillMount() {
// maps azimuth and elevation for all objects
fetch('http://localhost:5001/PositionComponent/').then(results => {
return results.json();
}).then(data => {
let positionStatus = data.map((positioning) => {
return (
<div className="temporary" key={positioning.key}>{positioning.Elevation.positionValue} : {positioning.Azimuth.positionValue}</div>
);
});
this.setState({ positionStatus: positionStatus });
});
}
render() {
return (
<div className="location">
<div className="col-xs-12">
{this.state.positionStatus}
</div>
</div>
);
}
}
export default StatusBar;
The relevant JSON has been provided for context of the issue, but is being served from JSON.server for sake of development speed. It seems that the issue is that I am receiving Object {key: 0, Elevation: Object } Object { key: 1, Azimuth: Object } where I cannot go down a level into the object itself by calling {positioning.Elevation.positionValue}. My question is, how would I go about mapping this so that I can get those interior object values tied to Elevation, and Azimuth?
data.PositionComponent.map(...)instead ofdata?console.log(data)before mapping?positioning.Elevationmay or may not be defined. If you only ever haveElevationorAzimuth, then you can do:{(positioning.Elevation) ? positioning.Elevation.positionValue : positioning.Azimuth.positionValue}