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

92
Visualizações
How to filter and map a list of items by targeting a property with javascript

Im working on a ReactJS project and in my data structure, i have 2 buttons: ATM, and PARKING:

buttons: [
    {
      value: 'ATM',
      icon: 'atm',
      floors: [
        { 
          value: 'L1',
          floor: 0,
        },
        { 
          value: 'L2',
          floor: 0,
        },
      ]   
    },
    {
      value: 'PARKING',
      icon: 'parking',
      floors: [
        { 
          value: 'L1',
          floor: 0,
        },
        { 
          value: 'L2',
          floor: 0,
        },
      ]   
    },
   ],

I want to target the available floors values: 'L1', 'L2' of ATM and display it as individual titles on HTML.

My approach is first to apply a filter for ATM, then map out the floors like so:

const filteredButton = _.filter(buttons, {value : 'ATM'}); // Filter for ATM

const mappedAtm = filteredButton.map((item)=>(item.floors)) // Map for Floors

const mappedFloor = mappedAtm.map((floors)=>(floors.value)) // Map for Floors values

However, when I console.log(mappedFloor) it returns undefined

My expected output is: "L1", "L2"

Ideally if i get mappedFloor to work, i would then display it on html like so(on browser it should show 2 span tags):

<p className="floors">
  { 
    mappedFloor.map((items, i) => (
        <span key={i}>
          {items}
        </span>
    ))
  }
</p>

Any suggestions and tips are greatly appreciated.

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

0

Your filter returns an array, the first map makes it an array of array and the second expects it to be an array of objects.

To fix that, you can either use a Array.prototype.find instead of the filter:

buttons.find((e) => e.value === 'ATM').floors.map((e) => e.value)

Or treat your last map as an array of array of objects and not as an array of objects.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can also do that by making one common function that will run on button onClick event that will return that what you want is assume and in function passing an argument of array and property which you want to filter

// function that filter with respect to value property and return floors array, i assume that what you want
const filterFloors = (floorsArray,filteredProperty) => {
    return floorsArray.find((item) => {if (item.value === filteredProperty) return item.floors
    }).floors
    
} 

console.log(filterFloors(buttons,'ATM'));
console.log(filterFloors(buttons,'PARKING'))
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