日常笔记,js生成一些简单的假数据

    /**
     * @description: 随机生成数据
     * @param {Number} val 生成数据的条数
     * @return {Array} 返回生成的数据
     * @Date: 2020-04-26 16:00:00
    */
    getData(val=20){
      // 随机生成中文名字
      const randomName = () => {
        const nameList = ['张三','李四','王五','赵六','田七','周八','吴九','郑十']
        return nameList[Math.floor(Math.random() * nameList.length)]
      }
      // 随机生成标题
      const randomTitle = () => {
        const
          titleList = ['我是标题1','我是标题2','我是标题3','我是标题4','我是标题5','我是标题6','我是标题7','我是标题8','我是标题9','我是标题10'],
          index = Math.floor(Math.random() * titleList.length)
        return titleList[index]
      }
      // 随机生成30-180个字之间的中文内容
      const randomContent = () => {
        var content = ''
        var length = Math.floor(Math.random() * 150) + 30; // 生成30-180之间的随机数
        for (var i = 0; i < length; i++) {
          var word = Math.random() < 0.5 ? '你好' : '世界'
          content += word
        }
        return content
      }
      // 随机生成0和1的字符串
      const randomIsOwnDynamic = () => {
        const randomNumber = Math.random(); // 生成0到1之间的随机数  
        return Math.floor(randomNumber * 2); // 将随机数舍入到最接近的整数,得到0或
      }
      // 随机生成时间
      const randomTime = () => {
        const now = new Date() // 获取当前本地时间
        const year = now.getFullYear()
        const month = ('0' + (now.getMonth() + 1)).slice(-2)
        const day = ('0' + now.getDate()).slice(-2)
        const hour = ('0' + now.getHours()).slice(-2)
        const minute = ('0' + now.getMinutes()).slice(-2)
        const second = ('0' + now.getSeconds()).slice(-2)
        const formattedTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`
        return {
          year,
          month,
          day,
          hour,
          minute,
          second,
          formattedTime
        }
      }
      const list = []
      // 向数组中添加50条随机数据
      for(let i = 0; i < val; i++){
        // 循环随机生成数据
        const item = {
          id:md5(Math.random().toString()).toString(),
          creatorName:randomName(),
          title:randomTitle(),
          content:randomContent(),
          creator:md5(Math.random().toString()).toString(),
          isOwnDynamic:randomIsOwnDynamic(),
          createAt:randomTime().formattedTime,
          year:randomTime().year,
          month:randomTime().month,
          day:randomTime().day,
        }
        list.push(item)
      }
      return list
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值