Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda