skip to Main Content

for the following contents of data object:

0
: 
{pppId: '005618-00', eppo: 'FAUSY', crop: 'Rotbuche'}
1
: 
{pppId: '005618-00', eppo: 'PSTME', crop: 'Douglasie'}
2
: 
{pppId: '005618-00', eppo: 'QUESS', crop: 'Eiche'}
3
: 
{pppId: '005618-00', eppo: 'YLAGL', crop: 'Langholzpolter'}
4
: 
{pppId: '005618-00', eppo: 'PIEPU', crop: 'Blaufichte'}
5
: 
{pppId: '005618-00', eppo: 'ALUSS', crop: 'Erle'}
6
: 
{pppId: '005618-00', eppo: 'POPSS', crop: 'Pappel'}
7
: 
{pppId: '005618-00', eppo: 'YLAGE', crop: 'Einzelstämme'}
8
: 
{pppId: '005618-00', eppo: 'NNNZG', crop: 'Ziergehölze'}
9
: 
{pppId: '005618-00', eppo: 'ACRCA', crop: 'Acer campestre'}
10
: 
{pppId: '005618-00', eppo: 'YLAGS', crop: 'Schichtholzpolter'}
11
: 
{pppId: '005618-00', eppo: 'PIUSS', crop: 'Kiefer'}
12
: 
{pppId: '005618-00', eppo: 'ABINO', crop: 'Nordmann-Tanne'}
13
: 
{pppId: '005618-00', eppo: 'CIPBE', crop: 'Carpinus betulus'}
14
: 
{pppId: '005618-00', eppo: 'LAXSS', crop: 'Lärche'}

i want to filter out the contents of the object data by the crop name Eiche and Kiefer
i want to make data contains only the 2nd and the 11th entries. in other words, i want data to contain:

{pppId: '005618-00', eppo: 'QUESS', crop: 'Eiche'}
{pppId: '005618-00', eppo: 'PIUSS', crop: 'Kiefer'}

how can i achieve that please

2

Answers


  1. You can try the following way:

    const data = {
      '0': { pppId: '005618-00', eppo: 'FAUSY', crop: 'Rotbuche' },
      '1': { pppId: '005618-00', eppo: 'PSTME', crop: 'Douglasie' },
      '2': { pppId: '005618-00', eppo: 'QUESS', crop: 'Eiche' },
      '3': { pppId: '005618-00', eppo: 'YLAGL', crop: 'Langholzpolter' },
      '4': { pppId: '005618-00', eppo: 'PIEPU', crop: 'Blaufichte' },
      '5': { pppId: '005618-00', eppo: 'ALUSS', crop: 'Erle' },
      '6': { pppId: '005618-00', eppo: 'POPSS', crop: 'Pappel' },
      '7': { pppId: '005618-00', eppo: 'YLAGE', crop: 'Einzelstämme' },
      '8': { pppId: '005618-00', eppo: 'NNNZG', crop: 'Ziergehölze' },
      '9': { pppId: '005618-00', eppo: 'ACRCA', crop: 'Acer campestre' },
      '10': { pppId: '005618-00', eppo: 'YLAGS', crop: 'Schichtholzpolter' },
      '11': { pppId: '005618-00', eppo: 'PIUSS', crop: 'Kiefer' },
      '12': { pppId: '005618-00', eppo: 'ABINO', crop: 'Nordmann-Tanne' },
      '13': { pppId: '005618-00', eppo: 'CIPBE', crop: 'Carpinus betulus' },
      '14': { pppId: '005618-00', eppo: 'LAXSS', crop: 'Lärche' },
    };
    
    //convert object to array
    const dataArray = Object.values(data);
    
    //filter the array based on 'crop' property
    const filteredArray = dataArray.filter(entry => entry.crop === 'Eiche' || entry.crop === 'Kiefer');
    
    //if you want the result back as an object
    const filteredData = Object.fromEntries(filteredArray.map((entry, index) => [index.toString(), entry]));
    
    console.log(filteredData);
    Login or Signup to reply.
  2. Use Set to filter the needed crops:

    const data = [
      { pppId: '005618-00', eppo: 'FAUSY', crop: 'Rotbuche' },
       { pppId: '005618-00', eppo: 'PSTME', crop: 'Douglasie' },
       { pppId: '005618-00', eppo: 'QUESS', crop: 'Eiche' },
       { pppId: '005618-00', eppo: 'YLAGL', crop: 'Langholzpolter' },
       { pppId: '005618-00', eppo: 'PIEPU', crop: 'Blaufichte' },
       { pppId: '005618-00', eppo: 'ALUSS', crop: 'Erle' },
       { pppId: '005618-00', eppo: 'POPSS', crop: 'Pappel' },
       { pppId: '005618-00', eppo: 'YLAGE', crop: 'Einzelstämme' },
       { pppId: '005618-00', eppo: 'NNNZG', crop: 'Ziergehölze' },
       { pppId: '005618-00', eppo: 'ACRCA', crop: 'Acer campestre' },
       { pppId: '005618-00', eppo: 'YLAGS', crop: 'Schichtholzpolter' },
       { pppId: '005618-00', eppo: 'PIUSS', crop: 'Kiefer' },
       { pppId: '005618-00', eppo: 'ABINO', crop: 'Nordmann-Tanne' },
       { pppId: '005618-00', eppo: 'CIPBE', crop: 'Carpinus betulus' },
       { pppId: '005618-00', eppo: 'LAXSS', crop: 'Lärche' },
    ];
    
    const filter = new Set( ['Eiche' , 'Kiefer']);
    const filteredArray = data.filter(({crop}) => filter.has(crop));
    
    
    console.log(filteredArray);

    An one-liner:

    const data = [
      { pppId: '005618-00', eppo: 'FAUSY', crop: 'Rotbuche' },
       { pppId: '005618-00', eppo: 'PSTME', crop: 'Douglasie' },
       { pppId: '005618-00', eppo: 'QUESS', crop: 'Eiche' },
       { pppId: '005618-00', eppo: 'YLAGL', crop: 'Langholzpolter' },
       { pppId: '005618-00', eppo: 'PIEPU', crop: 'Blaufichte' },
       { pppId: '005618-00', eppo: 'ALUSS', crop: 'Erle' },
       { pppId: '005618-00', eppo: 'POPSS', crop: 'Pappel' },
       { pppId: '005618-00', eppo: 'YLAGE', crop: 'Einzelstämme' },
       { pppId: '005618-00', eppo: 'NNNZG', crop: 'Ziergehölze' },
       { pppId: '005618-00', eppo: 'ACRCA', crop: 'Acer campestre' },
       { pppId: '005618-00', eppo: 'YLAGS', crop: 'Schichtholzpolter' },
       { pppId: '005618-00', eppo: 'PIUSS', crop: 'Kiefer' },
       { pppId: '005618-00', eppo: 'ABINO', crop: 'Nordmann-Tanne' },
       { pppId: '005618-00', eppo: 'CIPBE', crop: 'Carpinus betulus' },
       { pppId: '005618-00', eppo: 'LAXSS', crop: 'Lärche' },
    ];
    
    const filteredArray = data.reduce((r, item) => (r.set.has(item.crop) && r.arr.push(item), r), {set: new Set( ['Eiche' , 'Kiefer']), arr:[]}).arr;
    
    
    console.log(filteredArray);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search