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

141
Visualizações
Can I convert this into length all at once?

This is some part of the JSON. There are other data where the selected genre is book. Hence, I wanted to have an example of how to filter the Movie and count the length of the genre too.

const data = [{
      "1": { drama: { Comedy: true, Romance: true } },
      "2": {
        drama2: { Tragedy: true}
        others: "Dystopian",
      },
      id: "zaMR9TR7hNV3p3VFNumyNbXMto93",
      genre: {
        selected: "Movie",
      },
      displayName: "p1"
    }, 
     {
      "1": { drama: { Tragedy: true } },
      "2": {
        drama2: { Romance: true}
      },
      id: "zaMR9TR7hNV3p3VFNumyNbXMto93", //sample id
      genre: {
        selected: "Movie",
      },
      displayName: "p2"
    }]

 const drama = data.filter(
    (v) => v.genre?.selected == "Movie"
  )
  
  const comedy = drama.filter(
    (a) =>
    a["1"]?.drama.Comedy == true
  );

  const romance = drama.filter(
    (a) =>
    a["1"]?.drama.Romance == true
  );

   const tragedy = drama.filter(
        (a) =>
        a["1"]?.drama.Tragedy == true
      );

Is there a way where I could turn the comedy, romance, and tragedy into a length all at once rather than something like this?

  let n = comedy.length
  console.log(n)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use Array.reduce to create an object, counts, containing the count for each genre.

const data = [{
  "1": { drama: { Comedy: true, Romance: true, BuddyComedy: false } },
  "2": {
    drama2: { Tragedy: true},
    others: "Dystopian",
  },
  id: "zaMR9TR7hNV3p3VFNumyNbXMto93",
  genre: {
    selected: "Movie",
  },
  displayName: "p1"
}, 
 {
  "1": { drama: { Tragedy: true } },
  "2": {
    drama2: { Romance: true}
  },
  id: "zaMR9TR7hNV3p3VFNumyNbXMto93", //sample id
  genre: {
    selected: "Movie",
  },
  displayName: "p2"
}]

const movies = data.filter(v => v.genre?.selected == "Movie");
const counts = movies.reduce((acc, cur) => {
    return Object.entries(cur["1"].drama).reduce((acc, [key,value]) => { 
        if (value) { 
            acc[key] = (acc[key] || 0) + 1;
        }
        return acc;
    }, acc)
}, {})

console.log('Counts:', counts)

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