0

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
asked Jan 31, 2020 at 7:21

4 Answers 4

1

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)

answered Jan 31, 2020 at 7:33

3 Comments

This solutions will take 0.47900390625ms as it's using map and reduce to traverse array. Complexity will increase with more data.
@TusharVaghela it's 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?
I suggest using 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.
1

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)

answered Jan 31, 2020 at 7:25

Comments

0

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
answered Jan 31, 2020 at 7:40

Comments

0

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);
answered Jan 31, 2020 at 7:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.