/**
* @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
}
日常笔记,js生成一些简单的假数据
于 2023-06-20 14:11:17 首次发布