I’m using useState
and axios
to perform API call and GET response and passing it to Component to render it.
const [state,setState] = useState([]);
const getCurrData = () => {
axios.get('working api endpoint url').then(res => setState(res.data))
}
Now using that response I’m passing it to the component
<KlineInfo myPasser={state} />
My KlineInfo
component content is this:
import React from 'react'
const KlineInfo = (myPasser) => {
return (
<div>
{console.log('Mapping')}
{Object.entries(myPasser)
.map( ([key, value]) => `My key is ${key} and my value is ${value}` )
}
</div>
)
}
export default KlineInfo
Console log output is:
> "['My key is myPasser and my value is [object Object]']"
Now, my problem:
The content I get in the KlineInfo
component is below:
{
myPasser: {
retCode: 0,
retMsg: "OK",
result: {
symbol: "KORN",
category: "look",
list: [
["1694444820000", "25125.5", "25166.5", "25119.5", "25159.5", "1096706", "43.63253731"],
["1694444760000", "25067", "25125.5", "25053", "25125.5", "760836", "30.32376935"],
["1694444700000", "25110", "25126.5", "25067", "25067", "1785068", "71.11550939"],
],
},
retExtInfo: {},
time: 1694444852507,
},
}
How do I loop over the properties to get to myPasser-> result-> list to be able to:
- Print out single list elements
[ ...],[...]
and also - Loop over individual elements of each list to store them in file and / or database?
In this case simple:
Object.entries(myPasser).map( ([key, value]) => `${key} ${value}`)
is not enough.
2
Answers
value
is the Object itself, so you can’t render that as is.You should add a second
map()
inside your code so you can loop overvalue.result.list.map
to render eachlist
:You could simply do:
Insert whatever logic you need inside the forEach function. For example, to iterate over the inner elements and operate on them: