I want to return the number of votes using array.length
in ReactJS and display the number of votes
Expected output: Votes: 2
I have a object like this:
"likes" : {
"like1" : {
"blogId" : 1,
"date" : "09-17-2021",
"id" : "randomID_12345",
"userId" : "evedave"
},
"like2" : {
"blogId" : 1,
"date" : "09-17-2021",
"id" : "randomID_123123",
"userId" : "evedarryle"
}
},
NOTE: This is returning as a object but I declare my state as a array
Reference :
Here's my code:
const blogItems = useSelector((state) => state.blogs);
const [blogData, setBlogData] = useState([]);
const [blogVotes, setBlogVotes] = useState([]);
useEffect(() => {
setBlogData(blogItems);
const finalVotesData = blogData.items?.find((blog) => {
return blog.id === 1;
});
setBlogVotes(finalVotesData?.likes);
}, [blogItems, blogData.items, blogVotes]);
console.log(blogVotes);
Here's the array.length
<p>{blogVotes.length}</p>
How can I display the number of votes using array.length
?