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

150
Visualizações
Problem with filtering javascript multidimensional array of object

I want to filter out data from a multilayered JSON object:

    var players = [
    0:{
        id: "1"
        stats: 
        { 
          yellow_cards: "0" 
          saves: "0" 
        }
        explain: 
        [
        0:{
            fixture: "251"   
            stats: [
            0:{
                identifier: "minutes"
                points: "2" 
                value: "75"
              }  
            ] 
          }
        1:{ 
            fixture: "191" 
            stats: 
            [
            0:{
                identifier: "minutes"
                points: "2" 
                value: "83"
              }  
            1:{
                identifier: "assists"
                points: "3"
                value: "1"
              }
            ]
          }
        ]
      }
    ]    

I want to sort out the players who have at least one object where identifier = "minutes" and the value is between 50 and 60. If I loop through everything it works fine:

var selected = [];
  for(i=0; i<players.length; i++){
    for(j=0; j<players[i].explain.length; j++){
      for(k=0; k<players[i].explain[j].stats.length;k++){
        if(players[i].explain[j].stats[k].identifier == "minutes"){
          if(players[i].explain[j].stats[k].value >50 && players[i].explain[j].stats[k].value < 60) { 
            selected.push(players[i]);
          }
        }
      }
    }
  }

This takes too much time, and I'm sure there is a more elegant way to do this with array filter and find function. Any help with this is highly appreciated.

Edit: In order to keep it short the array above shows only one object which doesn't fulfill the criteria.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of using this tradition for loop for nested filtering, you can achieve the same requirement via Array.filter() along with Array.some()

Here is the performance check with both the approaches : jsbench

enter image description here

Demo :

var players = [{
  id: "1",
  stats: { 
    yellow_cards: "0", 
    saves: "0" 
  },
  explain: [{
    fixture: "251", 
    stats: [{
      identifier: "minutes",
      points: "2",
      value: "75"
    }] 
  }, { 
    fixture: "191", 
    stats: [{
      identifier: "minutes",
      points: "2",
      value: "83"
    }, {
      identifier: "assists",
      points: "3",
      value: "1"
    }]
  }]
}];

const selected = players.filter(({ explain }) => {
  return explain.some(({ stats }) => {
    return stats.some(({ identifier, value }) => {
      return identifier === "minutes" && (value > 50 && value < 60)
    })
  })
})

console.log(selected);

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