¿Alguien puede ayudar y dar un ejemplo de lo que está sucediendo aquí?
Soy bastante nuevo en el campo de Javascript y TI y no puedo entender lo que está sucediendo. Por favor ayuda.
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);Es una forma muy detallada de hacer una cadena delimitada por comas a partir de algo iterable. No creo que sea una matriz 2D, más probablemente una matriz de objetos, pero necesita publicar la matriz para mostrarme
Para un merchantPanToString = merchantPanToString +
puede ser escrito
merchantPanToString +=
Tenga en cuenta que necesitamos acceder a los valores usando la notación de paréntesis [] debido a - en las teclas del elemento
Muestro un mapa en el segundo ejemplo.
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);