I am fetching an API from cricapi. after fetching, its object tree is like this
{matches: Array(215), v: "1", ttl: 299, provider: {…}, creditsLeft: 250}creditsLeft: 250matches: (215) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …][0 … 99][100 … 199][200 … 214]length: 215__proto__: Array(0)provider: {source: "Various", url: "https://cricapi.com/", pubDate: "2020-11-16T15:03:06.282Z"}ttl: 299v: "1"__proto__: Object
it has an integer 0 in its object tree. when i try to console.log the path
console.log(data.matches.0);
it gives an error
SyntaxError: G:\des\cricketserver\src\components\news.js: Unexpected token, expected "," (12:29)
I'm reading data in from an API and I'm trying to have the elements in the data array show up in a select tag in HTML using React, but it keeps telling me that the data is not an array even though it seems like it is when I do console.log.
render(){
console.log(this.state.countries.Countries)
return(
<div className = "tables-container">
<h1>Tables</h1>
<label>Please Select a Country to view data for: </label>
<select name = "countries" id = "countries">
<option selected = "selected">Select a Country</option>
{
this.state.countries.Countries.map((data)=>(
<option>{data.Country}</option>
))
}
</select>
</div>
)
}
When I run this, it says "TypeError: Cannot read property 'map' of undefined." The console.log result reads
(188) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
so I'm pretty sure this.state.countries.Countries should be an Array, so the map function should work, but it doesn't.
In my constructor I have
this.state = {
countries: [],
}
and I have the API fetch in
componentDidMount(){
fetch("https://api.covid19api.com/summary")
.then((data) => data.json())
.then((data) => {
this.setState({
countries: data
})
})
}
I've been stuck on this for an hour now I really need help figuring it out. Thanks!
this.state = {
countries: [],
}
this.state.countries starts off as an empty array. It has no .Countries property on it, so for your first render, this.state.countries.Countries is undefined, and you can't map undefined.
You need your state to have the same shape before and after you load data. So if you want the .countries.Countries nesting, change your initial state to this:
this.state = {
countries: {
Countries: []
}
}
If you just want one level of nesting, keep your initial values and change the following:
// In componentDidMount
this.setState({
countries: data.Countries
})
// In render
this.state.countries.map(
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to map over my API to get the nodes.
Here is the format my API is in:
[
{
"agent": "....",
"managementDomain": "...",
"node": "..."
},
{
"agent": "....",
"managementDomain": "...",
"node": "..."
},
...
]
The format I need to get it into for react-select:
const colourOptions = [
{ value: 'orange', label: 'Orange' },
{ value: 'yellow', label: 'Yellow' },
{ value: 'green', label: 'Green' },
];
How I am mapping over it (using Redux for state management):
const alarms = this.props.dataReducer.alarmDetails
const {test} = alarms.map(alarm => ({value:alarm.node, label: alarm.node}))
The error I am getting:
TypeError: Cannot read property 'node' of null
console.log(this.props.dataReducer.alarmDetails)
(288) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, null, {…}, {…}, {…}, {…}, null, {…}, {…}, null, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, null, null, null, {…}, {…}, {…}, {…}, {…}, null, {…}, {…}, {…}, {…}, {…}, {…}, …]
[0 … 99]
0:
agent: "..."
managementDomain: null
node: "..."
What's the best way to map over the API to get the node from each object within the array?
This error is actually self explanatory. Your map code is alright. Problem seem to exist that some of the objects in array is null or empty, so when javascript is trying to find node property on that object, its hitting the rock.
Actually this.props.dataReducer.alarmDetails this data contains null value, something like..
[
{
"agent": "....",
"managementDomain": "...",
"node": "..."
},
{
"agent": "....",
"managementDomain": "...",
"node": "..."
},
null
...
]
So, maybe you can filter the null values:
this.props.dataReducer.alarmDetails.filter(data=>data)
and put map code here
this.props.dataReducer.alarmDetails.filter(data=>data).map(d => ({value:d.node, label: d.node}))
You can return something like this:
import React from "react";
import ReactDOM from "react-dom";
import Select from "react-select";
import "./styles.css";
const alarms = [
{ agent: 1, managementDomain: null, node: "node1" },
{ agent: 2, managementDomain: null, node: "node2" }
];
let teamsFromApi = alarms.map(team => { return {value: team.agent, label: team.node} });
function App() {
return (
<div className="App">
<Select options={teamsFromApi} />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Note: Replace node data.
You can check the example Codebox
I have a json Data which looks like this
0: (963) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
1: (964) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
2: (954) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…},
where my data looks like this
[0 … 99]
0: {y: 0, x: 10.7279501}
1: {y: 0, x: 10.73239994}
2: {y: 0, x: 10.73684978}
3: {y: 0, x: 10.7413168}
4: {y: 0, x: 10.74576664}
5: {y: 0, x: 10.7502327}
Now, I am trying to use plotly, the plotly documentation for react states just this example
<Plot
data={[
{
x: [1, 2, 3],
y: [2, 6, 3],
type: 'scatter',
mode: 'lines+points',
marker: {color: 'red'},
},
/>
The above just to plot data for single graph, but if I do something like this
<Plot
data={[
{
x: [1, 2, 3],
y: [2, 6, 3],
type: 'scatter',
mode: 'lines+points',
marker: {color: 'red'},
},
{
x: [2, 5, 3],
y: [2, 8, 3],
type: 'scatter',
mode: 'lines+points',
marker: {color: 'red'},
}
]}
/>
I can plot multiple data.
Now, Can someone help me in mapping my data such that I can create multiple graphs?
or the least can someone please help me in figuring out how can I create multiple object inside array using map
{
//data
},
{
//data1
}
I tried this but it didn't work
data={[
this.graphdata.map((data, index) => ({
x: data["x"],
y: data["y"],
type: 'scatter',
mode: 'lines+points',
marker: {color: 'red'},
}))
This is an extended version of J. Hesters solution above.
You must store the new array returned by the .map method in a new variable, let's say, plottableData.
const plottableData = graphdata.map(data => ({
x: data.map(point => point.x),
y: data.map(point => point.y),
type: 'scatter',
mode: 'lines+points',
marker: { color: 'red' },
}));
Then, you can use the plottableData in your code as follows-
<Plot data={plottableData} />
Or
<Plot data={[...plottableData]} />
Hope this will also be helpful!
I assume graphdata equals this:
0: (963) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
1: (964) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
2: (954) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…},
Then you would have to do:
graphdata.map(data => {
// at this point data equals [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
return {
x: data.map(point => point.x),
y: data.map(point => point.y),
type: 'scatter',
mode: 'lines+points',
marker: {color: 'red'},
}
});