Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Group json data by a property - vue.js

I have a JSON I would like to re-arrange my data, so that I can group all items by category name. Here is my data.

[
  {
    "id":317,
    "name":"Ingram's Camphor Cream",
    "slug":null,
    "product_category_id":15,
    "industry_id":2,
    "deleted_at":null,
    "category":{
      "id":15,
      "name":"Skincare",
      "slug":null,
      "industry_id":2,
      "deleted_at":null,
      "sku_id":3,
      "is_active":1
    },
  },
  {
    "id":323,
    "name":"Zimgold",
    "slug":null,
    "product_category_id":2,
    "industry_id":2,
    "category":{
      "id":2,
      "name":"Cooking Oils",
      "slug":null,
      "industry_id":2,
      "sku_id":2,
      "is_active":1
    }
  }
]

I would like to have a result like so:

[
   'Skincare': [ {}],
   'Cooking Oils': [{}]
]

Thank you in advance. I would really appreciate your help.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can reduce the array of products to a map of names to product arrays with a reducer like this:

const grouped = products.reduce((grouped, product) => {
  const key = product.category.name
  // replace the array at [key] with a new one including the current element
  return { ...grouped, [key]: (grouped[key]??[]).concat(product)}
}, {})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe there is a a prettier way but something like this would work

 var groupedData = {};

    data.forEach(item => {
        if (!groupedData[item.category.name]) {
            groupedData[item.category.name] = [];
        }
        groupedData[item.category.name].push(item);
    })
about 4 years ago · Juan Pablo Isaza Denunciar

0

arr.map(item=>{
    let key = item.category.name
    return {[key]:item}
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda