1

I have tried my best the whole day but I can't find a working solution for my problem.

Here is it.

I have an object containing the list of folders and the number of files inside each one like this:

let Folders = {

  'listen': 0,
  'listen_again_long': 0,
  'intercations': 0,
  'watch_again': 0,

};

And an array named result that contains the list of files inside the parent folder of above object.

let result = [

"https://example.com/test/media/instructions/listen/1.mp3",
"https://example.com/test/media/instructions/listen/2.mp3",

"https://example.com/test/media/instructions/listen_again_long/1.mp3",
"https://example.com/test/media/instructions/listen_again_long/2.mp3",
"https://example.com/test/media/instructions/listen_again_long/3.mp3",
"https://example.com/test/media/instructions/listen_again_long/4.mp3",

"https://example.com/test/media/instructions/watch_again/1.mp3",

]

I want to count the files inside each folder (listen...listen_again_long...) based on information represented in result array and modify the numbers of Folders object...

For instance in the above sample codes we have 2 files inside listen and 4 inside listen_again_long and 1 inside watch_again and none in intercations. so we should get back the object like this:

let Folders = {

  'listen': 2,
  'listen_again_long': 4,
  'intercations': 0,
  'watch_again': 1,

};
foxer
  • 811
  • 1
  • 6
  • 16

2 Answers2

1

This might work:

let Folders = {
  'listen': 0,
  'listen_again_long': 0,
  'intercations': 0,
  'watch_again': 0,
};


let result = [
  "https://example.com/test/media/instructions/listen/1.mp3",
  "https://example.com/test/media/instructions/listen/2.mp3",

  "https://example.com/test/media/instructions/listen_again_long/1.mp3",
  "https://example.com/test/media/instructions/listen_again_long/2.mp3",
  "https://example.com/test/media/instructions/listen_again_long/3.mp3",
  "https://example.com/test/media/instructions/listen_again_long/4.mp3",

  "https://example.com/test/media/instructions/watch_again/1.mp3"
]

function updateFoldersCount(urls, folders){
  urls.forEach(v => {
    const folder = v.match(/instructions\/(.+)\//)[1]; // assuming that it will always be like ...instructions/{folder name}/...
    folders[folder] = folders[folder] ? folders[folder]+1 : 1; // in case of folder doesn't exist yet - it will create it
  });
}

updateFoldersCount(result, Folders);

console.log(Folders)
Michał Z.
  • 1,322
  • 1
  • 10
  • 17
  • Thanks for the answer. what if the folder names changes to `listen: 0, listen_again_last: 0, listen_again_long: 0, listen_again_short: 0,` – foxer May 05 '20 at 17:24
  • And ` 'lets_start': 0, 'try_again': 0, 'interact_again': 0,` – foxer May 05 '20 at 17:24
0

Iterate the object & check if the key like folderA,folderB is contained in any string from the result array. You can get the count of the strings which contains the keys bu using filter & length. Also instead of mutating the existing object inside a loop create a new object and assign the key & count

let folders = {

  'folderA': 0,
  'folderB': 0,
  'folderC': 0,
  'folderD': 0,

}

let result = [
  "https://example.com/test/media/instructions/folderA/1.mp3",
  "https://example.com/test/media/instructions/folderA/2.mp3",

  "https://example.com/test/media/instructions/folderB/1.mp3",
  "https://example.com/test/media/instructions/folderB/2.mp3",
  "https://example.com/test/media/instructions/folderB/3.mp3",
  "https://example.com/test/media/instructions/folderB/4.mp3",

  "https://example.com/test/media/instructions/folderD/1.mp3",
];

let newFolder = {};

for (let keys in folders) {
  let getCount = result.filter(item => item.includes(keys)).length
  newFolder[keys] = getCount
}

console.log(newFolder)
brk
  • 48,835
  • 10
  • 56
  • 78
  • Thanks for the answer.. that is exactly what I've done before... maybe I should change the examples to show you the problem... I don't want to simply use includes because these folders will be count together `listen: 0, listen_again_last: 0, listen_again_long: 0, listen_again_short: 0,` – foxer May 05 '20 at 17:10
  • Then use `String.match` – brk May 05 '20 at 17:11
  • @foxer https://stackoverflow.com/questions/5752829/regular-expression-for-exact-match-of-a-string – brk May 05 '20 at 17:12