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

121
Visualizações
2D Array Object to 1D array filter function

Use Case 1
Assuming i have 2dArray Object of

let arr = [{'getName':'Report1'},{'getName':'User'},{'getName':'report 2'},{'getName':'User'},{'getName':'User'}]
let _NotRequiredSheet = ['User','Report 254',...]

Im trying to optimise my script with functional programming which will return me an array of

 ['report1','report2']

The current Method im using which does not have any error is :

 for(let i =0;i < arr.length;i++){
            if(arr[i].getName != _NotRequiredSheet[0]){
                console.log(arr[i].getName)
            }
        }

But this will impact if _notrequiredSheet have a big list of what is not required

I tried using this approach which is using filter but since its 2dObject Array, im unsure how should this be implemented. What i did on my poc is

    //Approach 1 : Not Working 
    let result = arr.filter(function (arr) {
        return arr.getName != _NotRequiredSheet.values();
    })

    //Output should be as 1dArray['report1','report2'] , not (5) [{…}, {…}, {…}, {…}, {…}]
    console.log(result)



    //Approach 2 : Will output as 2D array with filtered value
    // Will require to hardcord the index which is not advisable
    let result = arr.filter(function (arr) {
        return arr.getName != _NotRequiredSheet[0];
    })
    console.log(result)

i wanted to check if there is any way i could pass on using for loop with filter function. Result should return as 1D array which is

['Report1','Report2']

Use case 1 is Solved

Use Case 2 : 2D Object Array Assuming data is declared as

    let arr2 = [
        {$0:{'Name':'Report1'}},
        {$0:{'Name':'Report2'}},
        {$0:{'Name':'User'}}
    ]

Result should show this on console.log (2) [{…}, {…}] , filter function will remove 'User' as its reflected in _NotRequiredSheet.

Using the syntax i wrote

let result = arr2.map(item => item.$0.Name).filter(Name => !_NotRequiredSheet.includes(Name))

This will return as a single array

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

0

You could filter your data with looking for unwanted values and map only the wanted property.

const
    data = [{ getName: 'Report1' }, { getName: 'User' }, { getName: 'report 2' }, { getName: 'User' }, { getName: 'User' }],
    _NotRequiredSheet = ['User', 'Report 254'],
    result = data
        .filter(({ getName }) => !_NotRequiredSheet.includes(getName))
        .map(({ getName }) => getName);

console.log(result);

With a Set

const
    data = [{ getName: 'Report1' }, { getName: 'User' }, { getName: 'report 2' }, { getName: 'User' }, { getName: 'User' }],
    _NotRequiredSheet = ['User', 'Report 254'],
    take = k => o => o[k],
    hasNot = s => v => !s.has(v),
    comp = f => g => o => f(g(o)),
    result = data
        .filter(
            comp(hasNot(new Set(_NotRequiredSheet)))(take('getName'))
        )
        .map(({ getName }) => getName);

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

I'd recommend using reduce() so you can return something based on _NotRequiredSheet.includes(cur.getName)

let arr = [{'getName':'Report1'},{'getName':'User'},{'getName':'report 2'},{'getName':'User'},{'getName':'User'}]
let _NotRequiredSheet = ['User','Report 254' ];

let res = arr.reduce((prev, cur) => {
    if (_NotRequiredSheet.includes(cur.getName)) {
      return prev;
    } else {
      return [ ...prev, cur.getName ];
    }
}, []);
console.log(res);

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