M
M
Mirrrta2020-05-01 16:16:22
JavaScript
Mirrrta, 2020-05-01 16:16:22

How to get data out of an array?

There is an array:

[
  {
    id: 1,
    name: "Ivan",
    type: "FirstName",
  },
  {
    id: 2,
    name: "Petrov",
    type: "LastName",
  },
  {
    id: 3,
    name: "Zhora",
    type: "FirstName",
  },
]


How can I correctly display this data on the page so that something like this happens:

5eac20d63ff87403118619.png

That is, group by field in the object?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Pastukhov, 2020-05-01
@Mirrta

You can use lodash Or_.groupBy(array, 'type')

const groupedByType = array.reduce((acc, item) => {
  const arr = acc[item.type] || []
  arr.push(item)
  acc[item.type] = arr
  return acc
}, {})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question