I'm fetching data in React using axios like the following :
Hooks
const [date, setdate] = React.useState([]);
useEffect(() => {
axios
.get("http://localhost:8000/api/profilevisit/all/" + params.id)
.then((response) => {
setdate(response.data.data);
let NewArray = date.map((item, index) => {
return <span>{item.date}</span>;
});
console.log(NewArray);
})
.catch((err) => console.log(err));
}, []);
console.log(response.data.data):
(2) [{…}, {…}]
0: {date: "2021-05-15", views: 15}
1: {date: "2021-05-16", views: 6}
length: 2
__proto__: Array(0)
I would want to retrive from that array only date
elements so I will have an array like this :
[2021-05-15, 2021-05-16]
I think I'm close but I can't put my finger on what's wrong as I created a loop in NewArray
to get item.date
but the NewArray
in console doesn't returns anything. Any ideas ?
Edit : API direct response
{
"data": [
{
"date": "2021-05-15",
"views": 15
},
{
"date": "2021-05-16",
"views": 5
}
]
}