I am trying to populate an array of objects in the UI which has repetitive properties of title with teacher and students value.
Please find the original data -
const data = [
{
id: "1",
title: "Principle",
name: "John Doe",
children: [
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
},
{
id: "random id",
title: "Teacher",
name: "Bruce Wayne",
children: [
{
id: "random id",
title: "Student",
name: "Jason Todd",
},
{
id: "random id",
title: "Student",
name: "Dick Grayson",
},
{
id: "random id",
title: "Student",
name: "Tim Drake",
},
{
id: "random id",
title: "Student",
name: "Jason Todd",
},
{
id: "random id",
title: "Student",
name: "Jason Todd",
},
{
id: "random id",
title: "Student",
name: "Jason Todd",
},
{
id: "random id",
title: "Student",
name: "Jason Todd",
},
],
},
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
},
{
id: "random id",
title: "Teacher",
name: "Barry Allen",
},
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
},
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
},
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
},
],
},
];
which contains principal, teacher, and students. For eg., if the teacher name is repetitive and is coming like for eg., 5 times in data for Clark Kent, then I wish to restrict the data to show 3 entries and add one object, to specify that there are more entries with this name. like below -
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
more: true,
remaining: 2,
},
In the end, so my data can look like the below format to populate in UI -
const data = [
{
id: "1",
title: "Principle",
name: "John Doe",
children: [
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
},
{
id: "random id",
title: "Teacher",
name: "Bruce Wayne",
children: [
{
id: "random id",
title: "Student",
name: "Jason Todd",
},
{
id: "random id",
title: "Student",
name: "Dick Grayson",
},
{
id: "random id",
title: "Student",
name: "Tim Drake",
},
{
id: "random id",
title: "Student",
name: "Jason Todd",
},
{
id: "random id",
title: "Student",
name: "Jason Todd",
},
{
id: "random id",
title: "Student",
name: "Jason Todd",
more: true,
remaining: 2,
},
],
},
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
},
{
id: "random id",
title: "Teacher",
name: "Barry Allen",
},
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
},
{
id: "random id",
title: "Teacher",
name: "Clark Kent",
more: true,
remaining: 2,
},
],
},
];
Please help.
moreis added, will merge in UI as '+2 more Clark Kent' with+ ${remaining} ${name}, Click to view morein UI, so we can keep Clark Kent as 2