A
A
adizh2021-11-05 14:01:55
JavaScript
adizh, 2021-11-05 14:01:55

How to load local images in json?

I have a lot of pictures, and it would be cool to have just a json file, from where it could be loaded into the dom through iteration, but I don’t know how to insert or load local pictures into json.
61850ef88dcbd751356586.png61850f212acd6264540097.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Ternick, 2021-11-05
@Ternick

You can collect your pictures in one json file. It is desirable to optimize all pictures for size and quality)
Then they can be folded into base64 and packed into json.
Then iterate over json and create a house object. For example, the img tag and specify src in a similar way.

A
Aetae, 2021-11-05
@Aetae

JSON is obviously not designed for this.
But if you are building a webpack application (since React, then most likely), then it has a feature require.context , which allows you to pick up all the necessary files from the folder.

const r = require.context('./images/', false, /\.png$/);

const imagesMap = Object.fromEntries(r.keys().map((key) => [key, r(key)]));

console.log(imagesMap);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question