0

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
Janvier
  • 93
  • 11
  • 2
    What have you tried. Please give atleast 2 elements in your expected input so we can help you – Tushar Shahi Oct 08 '21 at 18:26
  • 1
    why the numbering on kigal are with `0` and the others it's without? – gilamran Oct 08 '21 at 18:29
  • the first appearing `city` to begin with `0` then increment the number in the same city – Janvier Oct 08 '21 at 18:37
  • Are you sure it shouldn't return either `['kigali01.jpg', 'kigali02.jpg', 'Kibuye01.jpg', 'kampala01.jpg']` or `['kigali1.jpg', 'kigali2.jpg', 'Kibuye1.jpg', 'kampala1.jpg']`? The changing style of the numbers if very odd. But also, where's your own attempt? Have you tried to solve this? What issues did you have? Please see [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – Scott Sauyet Oct 08 '21 at 21:00
  • `0` has to be appended on the first city only and the similar photo taken in the same city @ScottSauyet – Janvier Oct 08 '21 at 21:09
  • I think you really need to give another example or two. Also, please show your own attempts. – Scott Sauyet Oct 08 '21 at 21:21

2 Answers2

1

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)
BadPiggie
  • 5,471
  • 1
  • 14
  • 28
1
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
})
JOo
  • 17
  • 5
  • Can you please explain your code? How does it produce the expected output? – BadPiggie Oct 08 '21 at 18:49
  • first i did a mapping on the photos array next i took the format from the index one then i altred the first index to the city name + the format – JOo Oct 08 '21 at 19:01
  • The replicated name should have numbers. I think that missing in your code. – BadPiggie Oct 08 '21 at 19:24