Heim > Fragen und Antworten > Hauptteil
初涉 Js,想生成一组随机数据,基本的数据格式如下:
var test_data = { "data": [{ "seq": 36687, "vol": [63.27994918823242, 63.282066345214844, 63.28112030029297, 63.27827453613281, 63.27593231201172], "time": "2012-07-18-21-42-13-034", "hrate": 0, "status": 0 },{ "seq": 36688, "vol": [63.24958038330078, 63.249080657958984, 63.25041580200195, 63.25270080566406, 63.254817962646484], "time": "2012-07-18-21-42-13-072", "hrate": 0, "status": 0 }], };
问题: 我想随机生成 test_data,大概10万行, 纯js代码, seq是自增1,vol为5个随机数字,hrate和status均为0不变,time则是最后3个数字随机变化 (例子中为 42-13-034随机变化)
迷茫2017-03-08 09:27:20
<script> var test_data = { data: [] }; var data = test_data.data; for(var i = 36687; i < 36687 + 100000; i++){ var temp = { seq: i, vol: getVol(), time: "2012-07-18-21-42-13-0" + getNum(), hrate: 0, status: 0 } data.push(temp) } function getNum(){ var num = parseInt(Math.random()*100) if(num < 10){ num = "0" + num; } return num.toString(); } function getVol(){ var arr = [], i = 0; while(i < 5){ arr.push(Math.random()*100); i++; } return arr; } </script>
循环即可;