0

I have a project, that responds with items that user searches. Each item has its type and meta number (404-0. 404 for type, 0 for meta). Also I have a folder with item images, image name corresponds to the type and meta number of the item. How can I get through all image folder and extract only its name (404-0 without .img) ?

[
  {
    "type": 0,
    "meta": 0,
    "name": "Air",
    "text_type": "air"
  },
  {
    "type": 1,
    "meta": 0,
    "name": "Stone",
    "text_type": "stone"
  },
  {
    "type": 1,
    "meta": 1,
    "name": "Granite",
    "text_type": "stone"
  },
  {
    "type": 1,
    "meta": 2,
    "name": "Polished Granite",
    "text_type": "stone"
  },
  {
    "type": 1,
    "meta": 3,
    "name": "Diorite",
    "text_type": "stone"
  },
  {
    "type": 1,
    "meta": 4,
    "name": "Polished Diorite",
    "text_type": "stone"
  }
]

image folder

  • You need to use the fs module or the global pattern. https://nodejs.org/api/fs.html I'm pretty sure, however, that with a bit more effort you could've come up with a slightly preciser scenario, it doesn't really look like you put much effort before asking, in my opinion. – briosheje Aug 24 '20 at 10:28
  • Does this answer your question? [Looping through files in a folder Node.JS](https://stackoverflow.com/questions/32511789/looping-through-files-in-a-folder-node-js) – briosheje Aug 24 '20 at 10:29

1 Answers1

0

Have you tried? const files = fs.readdirSync('path/to/folder').map(name => name.split('.')[0])

Konrad
  • 21,590
  • 4
  • 28
  • 64