I am learning react as beginner , I am trying to create an array of products object. I have created it in a js file. Then exported the array. In another file I have imported and it works. Problem is whenever I refresh the page it gets new random values. As I have use random function to generate the object values. What I want is, after generating the array of objects once, it won't generate randomly further. How can I do that?
export const cosmetics = [];
const names = [];
const prices = [];
//random generated name list---------------------------------
function set_name() {
const letters = 'abcdefghijklmnopqrstuvwxyz'
for (let j = 0; j < 20; j++) {
let name = '';
for (let i = 0; i < 4; i++) {
name += letters.charAt(Math.floor(Math.random() * 26))
}
names.push(name)
}
}
// random generate prices list ---------------------------
function set_price() {
for (let j = 0; j < 20; j++) {
prices.push(Math.floor(Math.random() * 500) + 1000)
}
}
// create data set array of objects
function construct_data() {
for (let i = 0; i < 20; i++) {
const cosmetic = {
id: i + 1,
name: names[i],
price: prices[i]
}
cosmetics.push(cosmetic)
}
}
set_name();
set_price();
construct_data();
Aucun commentaire:
Enregistrer un commentaire