I would like to map the object and get the number/length of replies that can be found in replies
in this case 2. I have fetched this data from the API and is unable to get the number of replies that can be found here.
let data = [
{
"feedbackId":32,
"sender":"12345"
"comment":"Test Comment",
"replies":[
{
"feedbackLogId":32,
"feedbackReplyLogId":1,
"comments":"So this is 1",
"createdBy":"Jack",
},
{
"feedbackLogId":32,
"feedbackReplyLogId":2,
"comments":"2nd one",
"createdBy":"Min",
}
],
}
]
This is what I've done. I am only able to get 1
as the length.
let repliesToRender = data.map((item) => {item.replies})
let numReplies = repliesToRender.length
4 Answers 4
You can calculate reply per feedback using Array.prototype.map
for calculting total number of replies you can use Array.prototype.reduce
let data = [{"feedbackId":32,"sender":"12345","comment":"Test Comment","replies":[{"feedbackLogId":32,"feedbackReplyLogId":1,"comments":"So this is 1","createdBy":"Jack"},{"feedbackLogId":32,"feedbackReplyLogId":2,"comments":"2nd one","createdBy":"Min"}]},{"feedbackId":32,"sender":"12345","comment":"Test Comment","replies":[{"feedbackLogId":32,"feedbackReplyLogId":1,"comments":"So this is 1","createdBy":"Jack"},{"feedbackLogId":32,"feedbackReplyLogId":2,"comments":"2nd one","createdBy":"Min"}]}]
let repliesCount = data.map(({replies}) => replies.length);
console.log('per replies',repliesCount);
let totalCount = data.reduce((a,c) => a + c.replies.length, 0);
console.log('total replies ',totalCount)
3 Comments
map
and reduce
to traverse array. Complexity will increase with more data.map
OR reduce
depending upon the requirement, also what do you suggest otherwise? or its just an information? also how the complexity will increase with more data?let {replies} = data[0];console.log(replies.length);
This solution will take around 0.209716796875ms. With more number of replies, map
or reduce
will take more time.You have 2 issue in your code,
let repliesToRender = data.map((item) => {item.replies})
should be
let repliesToRender = data.map((item) => item.replies)
And map return you array so your repliesToRender will be an array of array so you need
let numReplies = repliesToRender[0].length
Or just
let numReplies = data.replies.length
let data = [
{
"feedbackId":32,
"sender":"12345",
"comment":"Test Comment",
"replies":[
{
"feedbackLogId":32,
"feedbackReplyLogId":1,
"comments":"So this is 1",
"createdBy":"Jack",
},
{
"feedbackLogId":32,
"feedbackReplyLogId":2,
"comments":"2nd one",
"createdBy":"Min",
}
],
}
]
let repliesToRender = data.map((item) => item.replies)
let numReplies = repliesToRender[0].length
console.log(numReplies)
Comments
If replies count is needed for each of the objects, then below will return array of replies count for respective objects in order,
let eachObjectsRepliesCount = data.map(({replies}) => replies.length);
If replies count is needed for all the objects combined, then below will return the total replies count,
let repliesToRender = data.map((item) => item.replies)
let totalRepliesCount = repliesToRender[0].length
Comments
Another approach. This will run faster in case of large data-sets, as it's not using loops.
let data = [
{
"feedbackId":32,
"sender":"12345",
"comment":"Test Comment",
"replies":[
{
"feedbackLogId":32,
"feedbackReplyLogId":1,
"comments":"So this is 1",
"createdBy":"Jack",
},
{
"feedbackLogId":32,
"feedbackReplyLogId":2,
"comments":"2nd one",
"createdBy":"Min",
}
],
}
]
let {replies} = data[0];
console.log(replies.length);