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

116
Vistas
JavaScript sort with reduce

In my movies array with reduce function I'm trying to make genre of the book an id and if the book has more than one genre the first one should be used as id. The code I wrote is not working the way it should. What is wrong with my code and how can I make it work the correct way

const movies = [
    {
        title: 'Harry Potter',
        genre: 'fantasy'
    },
    {
        title:'Bridgerton',
        genre:'romance'
    },
    {
        title: 'Chesnut Man',
        genre: 'crime'
    },
    {
        title: 'Little Women',
        genre: 'romance'
    },
    {
        title:'The Mask',
        genre: 'comedy'

    },
    {
        title:'Holidate',
        genre: ['comedy', 'romance']

    }]

`

/*

//correct output
{
  fantasy: { title: 'Harry Potter', genre: 'fantasy' },
  romance: {{ title: 'Little Women', genre: 'romance' },{ title: 'Holidate', genre: 'romance' },}}
  crime: { title: 'Chesnut Man', genre: 'crime' },
  comedy: {{ title: 'The Mask', genre: 'comedy'},{ title: 'Holidate', genre: 'comedy' } }
}

*/

// my code

const genreSort = movies.reduce(function(acc, curBook){
    if(Array.isArray(curBook.genre)){
        return {...acc,[curBook.genre[0]]:curBook}
    }
    else if(curBook.genre){
        return {...acc,[curBook.genre]:curBook}
        
    }
    return acc
    
},{})
console.log(genreSort)

`

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

0

You;re overwriting your single element for each group every time. You need an array there.

const movies = [{
        title: 'Harry Potter',
        genre: 'fantasy'
    },{
        title:'Bridgerton',
        genre:'romance'
    },{
        title: 'Chesnut Man',
        genre: 'crime'
    },{
        title: 'Little Women',
        genre: 'romance'
    },{
        title:'The Mask',
        genre: 'comedy'
    },{
        title:'Holidate',
        genre: ['comedy', 'romance']

    }];

const genreSort = movies.reduce(function(acc, curBook){
    let key = Array.isArray(curBook.genre) ? curBook.genre[0] : curBook.genre
    var arr = acc[key] || []
    arr.push(curBook);
    acc[key] = arr;
    return acc
    
},{})
console.log(genreSort)

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