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

96
Visualizações
Parsing 2D Array In Javascript

Can someone please help and give an example of what is happening here.

I am fairly new to Javascript and IT field and unable to understand what is happening. Please help.

let merchantPanToString = '';
for (let i = 0; i < merchantIdList.length; i++) {
  merchantPanToString = merchantPanToString + merchantIdList[i]['ref-id'] + ',' + merchantIdList[i]['ref-value'];
  if (i != merchantIdList.length - 1) {
    merchantPanToString = merchantPanToString + ',';
  }
}

console.log(merchantPanToString);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is a very verbose way to make a comma delimited string out of something iterable. I do not think it is a 2D array, more likely an object array but you need to post the array to show me

For one merchantPanToString = merchantPanToString +

can be written

merchantPanToString +=

Note we need to access the values using [] bracket notation because of the - in the item keys

I show a map in the second example

const merchantIdList = [
{ 'ref-id' : 'AId', 'ref-value' : 'AVal' },
{ 'ref-id' : 'BId', 'ref-value' : 'BVal' },
{ 'ref-id' : 'CId', 'ref-value' : 'CVal' },
{ 'ref-id' : 'DId', 'ref-value' : 'DVal' }
];

let merchantPanToString = '';
for (let i = 0; i < merchantIdList.length; i++) { // loop from 0 to but not including the length of the array
  merchantPanToString = merchantPanToString + // concatenate merchantPanToString to previous merchantPanToString
  merchantIdList[i]['ref-id'] + ',' + merchantIdList[i]['ref-value']; // plus the two values
  if (i != merchantIdList.length - 1) { // ugly way to NOT add a comma at the end
    merchantPanToString = merchantPanToString + ',';
  }
}
console.log(merchantPanToString);

// modern way - no let here because I reuse the variable from above so you do need a let or const in your code:
// const merchantPanToString = merchantIdList ...
merchantPanToString = merchantIdList
  .map(item => `${item['ref-id']},${item['ref-value']}`) // using template literals to join the values
  .join(','); // join the comma pairs with comma
console.log(merchantPanToString);

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