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

148
Visualizações
How to manipulate array of objects without loop

I have a code like below and it is working fine.

 for(let i=0;i< this.Array.length ; i++){
          if(this.Array[i].propertyObject.hasOwnProperty('header'))
          this.Array[i].ColumnName = this.Array[i].propertyObject.header;
    }

May i know how to achieve the same with Map. Thanks in advance.

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

0

May i know how to achieve the same with Map

I assume you mean map. map isn't the right tool for doing exactly what that loop does, because that loop modifies the array in place, but map creates a new array instead.

If you want a new array, perhaps also with new objects (e.g., functional programming or immutable programming):

// Replace `this.Array` with a new array
this.Array = this.Array.map(element => {
    // If we need to change this element...
    if (element.propertyObject.hasOwnProperty("header")) {
        // ...do a shallow copy along with the replacement
        element = {...element, ColumnName: element.propertyObject.header};
    }
    return element;
});

Note that that assumes the elements are simple objects. If they aren't, you'll need to handle constructing the replacement differently than just using {...original}.

But if you want to keep the same array as your current code does, your loop is just fine. You have other options (like forEach or for-of), but what you have is also fine. for-of is well-suited to what you're doing:

for (const element of this.Array) {
    if (element.propertyObject.hasOwnProperty("header")) {
          element.ColumnName = element.propertyObject.header;
    }
}

Side note: In new code, you might want to use Object.hasOwn rather than Object.prototype.hasOwnProperty (with a polyfill if needed for older environments; recent versions of all modern browsers support it natively, though).

about 4 years ago · Juan Pablo Isaza Relatório

0

this.Array.forEach((item, index, arr)=> {
       item.ColumnName = "header" in item ? item.header : item.ColumnName;
});
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