![]() |
沉稳的消防车 · 黄亦玫同款发色染发剂-黄亦玫同款发色染发剂促 ...· 1 月前 · |
![]() |
打酱油的排球 · 电影「假面骑士零一REAL x ...· 5 月前 · |
![]() |
机灵的铁链 · 中国人民大学阳光招生信息平台· 6 月前 · |
![]() |
想出家的水煮鱼 · 北京大兴机场接机服务价格表_北京包车· 8 月前 · |
我想使用另一个数组中的值通过
for
循环创建一个对象数组。
下面的代码片段生成
5
值,而不是
6
(根据需要)
function generateArray() {
var names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
var newObj = [];
for (i = 0; i < names.length - 1; i++) {
newObj[i] = {
name: names[(Math.floor(Math.random() * (names.length)))],
age: Math.floor(Math.random() * 40),
communication: Math.floor(Math.random() * 20),
skill: Math.floor(Math.random() * 20),
experience: Math.floor(Math.random() * 20)
return newObj;
console.log(generateArray());
如何生成与
names
数组中的值一样多的值?
发布于 2018-08-25 09:44:44
解决方案 -将
i < names.length - 1
替换为i < names.length
在
for
循环中执行代码块的条件错误。你的代码运行得很好,只是生成的结果比需要的少了1个。
MDN web docs
介绍了
for
的工作原理。
function generateArray() {
names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
newObj = [];
for (i = 0; i < names.length; i++) {
newObj[i] = {
name: names[(Math.floor(Math.random() * (names.length)))],
age: Math.floor(Math.random() * 40),
communication: Math.floor(Math.random() * 20),
skill: Math.floor(Math.random() * 20),
experience: Math.floor(Math.random() * 20)
return newObj;
console.log(generateArray());
发布于 2018-08-25 09:26:37
names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
arob = [
name: names[(Math.floor(Math.random() * (names.length)))],
age: Math.floor(Math.random() * 40),
communication: Math.floor(Math.random() * 20),
skill: Math.floor(Math.random() * 20),
experience: Math.floor(Math.random() * 20)
name: names[(Math.floor(Math.random() * (names.length)))],
age: Math.floor(Math.random() * 40),
communication: Math.floor(Math.random() * 20),
skill: Math.floor(Math.random() * 20),
experience: Math.floor(Math.random() * 20)
name: names[(Math.floor(Math.random() * (names.length)))],
age: Math.floor(Math.random() * 40),
communication: Math.floor(Math.random() * 20),
skill: Math.floor(Math.random() * 20),
experience: Math.floor(Math.random() * 20)
name: names[(Math.floor(Math.random() * (names.length)))],
age: Math.floor(Math.random() * 40),
communication: Math.floor(Math.random() * 20),
skill: Math.floor(Math.random() * 20),
experience: Math.floor(Math.random() * 20)
name: names[(Math.floor(Math.random() * (names.length)))],
age: Math.floor(Math.random() * 40),
communication: Math.floor(Math.random() * 20),
skill: Math.floor(Math.random() * 20),
experience: Math.floor(Math.random() * 20)
name: names[(Math.floor(Math.random() * (names.length)))],
age: Math.floor(Math.random() * 40),
communication: Math.floor(Math.random() * 20),
skill: Math.floor(Math.random() * 20),
experience: Math.floor(Math.random() * 20)
];
在for循环中:
i < names.length;
,而不是
i < names.length - 1;
function generateArray() {
names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
newObj = [];
for(i=0; i < names.length; i++) {
newObj[i] = {
name: names[(Math.floor(Math.random() * (names.length)))],
age: Math.floor(Math.random() * 40),
communication: Math.floor(Math.random() * 20),