Randomly generate a name and randomly generate a string of specified length

✨Function that generates names of specified length

  • ⌛ Usage scenario: When uploading pictures or saving files, you can use this function when you need the front-end to generate unique file names.
  • ⏳ Input parameter: length, the length of the string that needs to be generated
  • ⌛ Return value {string} random string, eg:13y32hbrhj
    /**
     * 生成随机名称
     * @param length 随机名称长度
     * @returns {string} 13y32hbrhj
     */
    randomString(length) {
    
    
      const str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
      let result = '';
      for (let i = length; i > 0; --i)
        result += str[Math.floor(Math.random() * str.length)];
      return result;
    },

Guess you like

Origin blog.csdn.net/qq2468103252/article/details/125959823