1

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.

2
  • 1
    Shouldn't the remaining property for Clark Kent be 1? Commented Sep 21, 2021 at 8:08
  • 1
    HI @PaoloTormon, the obj where more is added, will merge in UI as '+2 more Clark Kent' with + ${remaining} ${name}, Click to view more in UI, so we can keep Clark Kent as 2 Commented Sep 21, 2021 at 8:12

1 Answer 1

1

Some assumptions:

  1. id in "more" object is just copied from the first excessive person.

  2. No children field is expected in excessive persons.

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',
      },
    ],
  },
];

const result = reduceRepetitions(data, 'name', 3);
console.log(JSON.stringify(result, null, '  '));

function reduceRepetitions(array, checkKey, maxRepetitions) {
  const reduced = [];

  const counter = {};

  for (const person of array) {
    const checkValue = person[checkKey];
    counter[checkValue] ??= { count: 0, moreObject: null };
    counter[checkValue].count++;
    if (counter[checkValue].count <= maxRepetitions) {
      reduced.push({
        ...person,
        ...person.children ?
          { children: reduceRepetitions(person.children, checkKey, maxRepetitions) } :
          {},
      });
    } else if (!counter[checkValue].moreObject) {
      counter[checkValue].moreObject = { ...person, more: true, remaining: 1 };
      reduced.push(counter[checkValue].moreObject);
    } else {
      counter[checkValue].moreObject.remaining++;
    }
  }

  return reduced;
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.