How to change all object values to random string in Node.js

1 Answer

0 votes
function random_string(size) {
   const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
   const len = characters.length;
   let rnd = '';
   for (let i = 0; i < size; i++) {
      rnd += characters.charAt(Math.floor(Math.random() * len));
   }
   return rnd;
}
   
const obj = {
  id: 128905,
  name: "Albus Dumbledore",
  academicrank: "Professor"
};

Object.keys(obj).forEach((key, index) => {
  	obj[key] = random_string(12);
});

console.log(obj);
   
   
   
   
   
/*
run:
   
{
  id: 'jJbpsSFmkFgu',
  name: 'GEbTioCyZJkI',
  academicrank: 'RFzOZXHYRVcA'
}

   
*/

 



answered Apr 25, 2022 by avibootz

Related questions

1 answer 115 views
1 answer 123 views
1 answer 112 views
1 answer 136 views
1 answer 121 views
...