Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!