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

105
Vistas
How to filter array of object based on condition React JS

I am new to react js, I searched google, object append, object concatenation, shallow copy, a deep copy are there in JavaScript, But, I can't find out the merge the array of objects into a single array of objects.

How to filter an array of objects based on the type and save the response as a single array of objects?

    const options = ["Fruit", "Vegitables", "Drinks"];
        const mainArray=[{
        Type:'Fruit',
        Name:'apple',
        Amount:'20'
        },
        {
        Type:'Fruit',
        Name:'apple',
        Amount:'20'
        },
        {
        Type:'Vegitables',
        Name:'tomoto',
        Amount:'50'
        },
        {
        Type:'Vegitables',
        Name:'onion',
        Amount:'20'
        },
        {
        Type:'Drinks',
        Name:'Milk',
        Amount:'30'
        },
        {
        Type:'Drinks',
        Name:'Juice',
        Amount:'20'
        }
        ]
        ///Filtering By Type
        
        const filterByType=()=>{
        let allMergedData=[];
         selectedtypes?.map((type)=>{
            let mergedData=[];
            let filteredData= mainArray?.filter(x=>x.Type===type.toLowerCase());
            mergedData=[...filteredData];
            allMergedData=[...mergedData, ...filteredData];
         })
         return allMergedData;
        };
        
        return(
        <div>
        {options.map((option) => (
                    <MenuItem key={option} value={name}>
                      <Checkbox value={option}
                  onChange={handleChange}
                  checked={selected.includes(option)} />
                      <ListItemText primary={option} />
                    </MenuItem>
                  ))}
        </div>
        )

Excepted output:


    allMergedData:[{
            Type:'Fruit',
            Name:'apple',
            Amount:'20'
            },
            {
            Type:'Fruit',
            Name:'apple',
            Amount:'20'
            },
            {
            Type:'Vegitables',
            Name:'tomoto',
            Amount:'50'
            },
            {
            Type:'Vegitables',
            Name:'onion',
            Amount:'20'
            },]

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

0

Maybe this function could help, edited as your need:

function groupBy(arr, criteria) {
  const newObj = arr.reduce(function (acc, currentValue) {
    if (!acc[currentValue[criteria]]) {
      acc[currentValue[criteria]] = [];
    }
    acc[currentValue[criteria]].push(currentValue);
    return acc;
  }, {});
  return newObj;
}

Test it here:

https://jsfiddle.net/pmiranda/y9odgtjm/2/

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