简体   繁体   中英

Add directory contents to the array as an object

在此处输入图像描述
I have a file structure like on a image and code which read folders content like that:

var array = []
fs.readdir(__dirname + '/static/katalogi', function (err, files) {
files.forEach(function (fileName) {
    console.log(fileName);
    array.push(fileName)
   });  
});

I would like to create structure something like this:

var list = {kat1:[files from folder],
kat2:[files from folder],
kat3:[files from folder]}

Does someone know how to do that becouse i really don't understand objects in js

You can try following structure to fulfill your need to accessing array data:

var list = [
  { kat1: file },
  { kat2: file },
  { kat3: file },
]

And to iterate kat (ie kat1, kat2, etc) you can use FormData.

new fd = new FormData()
for(var i=0; i<n; i++){
   fd.append('kat'+i, file) // output=> {kat1: file}
   list.push(fd)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM