简体   繁体   English

获取对象内对象数组的长度

[英]getting the length of the array of objects inside an object

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.我想映射对象并获取在这种情况下可以在replies中找到的replies数量/长度


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.我只能得到1作为长度。

let repliesToRender = data.map((item) => {item.replies})
let numReplies = repliesToRender.length

You have 2 issue in your code,您的代码中有 2 个问题,

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并且 map 返回你的数组,所以你的 repliesToRender 将是一个数组数组,所以你需要

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)

You can calculate reply per feedback using Array.prototype.map您可以使用Array.prototype.map计算每个反馈的回复

for calculting total number of replies you can use Array.prototype.reduce为了计算回复总数,您可以使用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)

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);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM