简体   繁体   English

从 Json 数据创建复杂表

[英]create complex table from the Json data

I have a JSON that looks like this我有一个看起来像这样的 JSON

[
{
    "teacher": "teacher1",
    "student": "student1"
    },
{
   "teacher": "teacher1",
    "student": "student1"
    },
{
    "teacher": "teacher1",
    "student": "student1"
    },
{
    "teacher": "teacher2",
    "student": "student1"
    },
{
   "teacher": "teacher2",
    "student": "student2"
    }
]

I want to convert it in a way that it shows me the count for each teacher like this我想将它转换成这样的方式,它可以向我显示每个老师的计数

[
    {
        "teacherName": "teacher1",
        "teacherCount": "3"
    },
    {
        "teacherName": "teacher2",
        "teacherCount": "2"
    },
]

I am working on a node project where I need to print this data in a table.我正在开发一个节点项目,我需要在表格中打印这些数据。

You can build a Map (using .reduce() ), which is indexed/keyed by the teacher value.您可以构建一个 Map(使用 .reduce .reduce() ),它由teacher值索引/键控。 The value stored at the teacher is the count of the number of times that teacher has been seen in your array of objects.存储在teacher 中的值是在您的对象数组中看到teacher 的次数的计数。 You can then use Array.from() with the Map built using reduce to get each key-value pair from the map (where the key is the teacherName and value is the teacherCount).然后,您可以将Array.from()与使用 reduce 构建的Map使用,以从 map 获取每个键值对(其中键是teacherName,值是teacherCount)。 To get each key-value pair, you can use the mapping function of Array.from() and map each key-value ([key, value]) to an object like so:要获取每个键值对,您可以使用 Array.from() 和 map 每个键值 ([key, value]) 到 object 的映射 function ,例如:

 const data = [{ "teacher": "teacher1", "student": "student1" }, { "teacher": "teacher1", "student": "student1" }, { "teacher": "teacher1", "student": "student1" }, { "teacher": "teacher2", "student": "student1" }, { "teacher": "teacher2", "student": "student2" } ]; const res = Array.from(data.reduce((map, {teacher}) => { return map.set(teacher, (map.get(teacher) || 0) + 1); }, new Map), ([teacherName, teacherCount]) => ({teacherName, teacherCount})); console.log(res);

You can collect your counts into a unique set using reduce , incrementing the teacher if it exists, otherwise initiating it to 1. Then an example of how to format follows using map .您可以使用reduce将您的计数收集到一个唯一的集合中,如果教师存在则将其递增,否则将其初始化为 1。然后使用map来说明如何格式化的示例。

 let data = getData(); let teachers = data.reduce((acc, val) => { if (val.teacher in acc) acc[val.teacher]++; else acc[val.teacher] = 1; return acc; }, {}); let formatted = Object.entries(teachers).map( ([teacherName, teacherCount]) => ({ teacherName, teacherCount }) ); console.log(formatted); /*****************************************************************/ function getData() { return [{ "teacher": "teacher1", "student": "student1" }, { "teacher": "teacher1", "student": "student1" }, { "teacher": "teacher1", "student": "student1" }, { "teacher": "teacher2", "student": "student1" }, { "teacher": "teacher2", "student": "student2" } ] }

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

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