Home > Net >  Filtering from nested array
Filtering from nested array

Time:01-26

I have an array of data loaded from firebase and I would like to get the data filtered from the Participation array for people that are in , for example, "Contabilidad publica" .

[
    {Last: 'Craig', Name: 'Carlos', Participated: undefined, Score: 243},
    {Last: 'Antezana', Name: 'Kimberly', Participated: undefined, Score: 188},
    {
      Last: 'Avendaño Villarroel',
      Name: 'Leonel',
      Participated: undefined,
      Score: '234',
    },
    {
      Last: 'Craig',
      Name: 'Carlos',
      Participated: ['Contabilidad publica', 'Ing industrial'],
      Score: 78,
    },
    {
      Last: 'Avendaño',
      Name: 'Leonel',
      Participated: undefined,
      Score: undefined,
    },
    {Last: 'Hola ', Name: 'Leon', Participated: undefined, Score: undefined},
    {
      Last: 'Grand',
      Name: 'Espectro',
      Participated: ['Contabilidad publica'],
      Score: '213',
    },
  ];

my desired result should show only the ones that have "Contabilidad publica" in the participation array like this:

[
 {
          Last: 'Craig',
          Name: 'Carlos',
          Participated: ['Contabilidad publica', 'Ing industrial'],
          Score: 78,
        },

  {
          Last: 'Grand',
          Name: 'Espectro',
          Participated: ['Contabilidad publica'],
          Score: '213',
        },
      ];

CodePudding user response:

You can use filter array function to check any element in array contains your custom condition.

In this case you need to check 2 statements:

  • Check if Participated is an array

  • Check if Participated includes "Contabilidad Pública" with includes

    console.log(arr.filter(element => Array.isArray(element.Participated) && element.Participated.includes('Contabilidad publica')));

Furthermore you can create a dynamic function:

const filterByParticipated = (array, type) =>
   array.filter(element => 
      Array.isArray(element.Participated) &&
      element.Participated.includes(type)
   );

CodePudding user response:

I would do it like this:

function filterByParticipated(data, value) {
    return data.filter(obj => {
        return obj && Array.isArray(obj.Participated) && obj.Participated.includes(value);
    });
}

CodePudding user response:

We can use object destructuring to zero in on the property of interest - Participated, and give an initial value of [] when the value is either undefined or null, and the .includes() array method:

const desired = data.filter(
    ({Participated = []}) => 
    Participated.includes('Contabilidad publica')
);

DEMO

const data = [
    {Last: 'Craig', Name: 'Carlos', Participated: undefined, Score: 243},
    {Last: 'Antezana', Name: 'Kimberly', Participated: undefined, Score: 188},
    {
      Last: 'Avendaño Villarroel',
      Name: 'Leonel',
      Participated: undefined,
      Score: '234',
    },
    {
      Last: 'Craig',
      Name: 'Carlos',
      Participated: ['Contabilidad publica', 'Ing industrial'],
      Score: 78,
    },
    {
      Last: 'Avendaño',
      Name: 'Leonel',
      Participated: undefined,
      Score: undefined,
    },
    {Last: 'Hola ', Name: 'Leon', Participated: undefined, Score: undefined},
    {
      Last: 'Grand',
      Name: 'Espectro',
      Participated: ['Contabilidad publica'],
      Score: '213',
    },
  ];
  
const desired = data.filter(({Participated = []}) => Participated.includes('Contabilidad publica'));

console.log( desired );

  •  Tags:  
  • Related