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

201
Visualizações
Array reduce method is overwriting the first element

below is my function which have reduce method

    getFilters(gridFilters, columnList){
      const filtersArray = [];
      let filterObject = {};
      gridFilters.forEach((ele, i, arr) => {
        filtersArray.push(...arr[i].filters);
      });      
      const _filArr = filtersArray.reduce((acc, {field, operator, value}) => {        
        filterObject['columnName'] = field;   
        filterObject['filterValue1'] = value;
        filterObject['filterOperator1'] = operator;
        filterObject['filterValue2'] = '';
        filterObject['filterOperator2'] = ''; 
        columnList.push(filterObject);    
        return acc;
      }, {});    
      return columnList;

Here, filtersArray will have 2 array elements like'

    Array(2)    
    0:
        field: "objectType"
        operator: "contains"
        value: "lic"
        [[Prototype]]: Object
    1:
        field: "attributeDisplayName"
        operator: "contains"
        value: "date"
        [[Prototype]]: Object
        length: 2
        [[Prototype]]: Array(0)

But after pushing the values to the columnList array, I am seeing the second object (which has attributeDisplayName) is coming for both values. Basically the first object (which has objectType) is getting overwritten with second object in array.

This is how the columnList is getting overwritten

    Array(2)    
    0:
        columnName: "attributeDisplayName"
        filterOperator1: "contains"
        filterOperator2: ""
        filterValue1: "date"
        filterValue2: ""
        [[Prototype]]: Object
    1:
        columnName: "attributeDisplayName"
        filterOperator1: "contains"
        filterOperator2: ""
        filterValue1: "date"
        filterValue2: ""
        [[Prototype]]: Object
        length: 2
        [[Prototype]]: Array(0)

Can some one please suggest what I am doing wrong here. Thanks

about 4 years ago · Juan Pablo Isaza
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