Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
How can I loop 2D array and modify its values

Given 2D array of photos taken while John doe was on trips so I'm asked to rename pictures and replace actual name with city name where a picture was taken.

const photos = [
['photo.jpg', 'kigali','2013-09-05 14:08:09'],
['demmpa.jpg', 'kigali','2013-09-05 14:08:09'],
['third.jpg', 'kibuye','2013-02-05 12:08:09']
['forthpic.jpg', 'kampala','2013-02-05 12:08:09']
]
   photos.map((photo, index)=>{
    photo.filter((photo, index)=> console.log(photo))
    
  })

Actual output

"photo.jpg"
"kigali"
"2013-09-05 14:08:09"
"demmpa.jpg"
"kigali"
"2013-09-05 14:08:09"

Expected output

kigali01.jpg
kigali02.jpg
Kibuye1.jpg
kampala1.jpg
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can do it using map. And the occurrence will just keep the number of how many times the name is repeated.

Note: if you need to add leading zeros, Please check it out How to output numbers with leading zeros in JavaScript?

const photos = [
  ['photo.jpg', 'kigali','2013-09-05 14:08:09'],
  ['demmpa.jpg', 'kigali','2013-09-05 14:08:09'],
  ['third.jpg', 'kibuye','2013-02-05 12:08:09'],
  ['forthpic.jpg', 'kampala','2013-02-05 12:08:09']
];

const occurrence = {}

const newArray = photos.map(arr => {

  const ext = arr[0].split('.')[1]; // file extension

  if (occurrence[arr[1]]) { // check whether the name is already there
    occurrence[arr[1]]++;
    return `${arr[1]}${occurrence[arr[1]]}.${ext}`;
  }
  
  occurrence[arr[1]] = 1;
  return `${arr[1]}1.${ext}`;

});


console.log(newArray)

about 4 years ago · Juan Pablo Isaza Denunciar

0

let photos = [
['photo.jpg', 'kigali','2013-09-05 14:08:09'],
['demmpa.jpg', 'kigali','2013-09-05 14:08:09'],
['third.jpg', 'kibuye','2013-02-05 12:08:09'],
['forthpic.jpg', 'kampala','2013-02-05 12:08:09']
]

 photos = Array.from(photos).map((photo)=>{
 const format = photo[0].split('.')[1]
 photo[0]=photo[1]+'.'+format
 return photo
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda