![]() |
玉树临风的乌冬面 · 萧山九中正式更名为杭州市钱塘高级中学· 8 月前 · |
![]() |
暗恋学妹的小摩托 · 迪士尼“吞下”21世纪福克斯重塑好莱坞格局 ...· 10 月前 · |
![]() |
聪明伶俐的青蛙 · 两部伦理片成就了姜恩惠,《善良的小姨子》是巅 ...· 11 月前 · |
![]() |
调皮的硬盘 · SQL面试题目汇总 - 知乎· 1 年前 · |
我想使用另一个数组中的值通过
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),
![]() |
玉树临风的乌冬面 · 萧山九中正式更名为杭州市钱塘高级中学 8 月前 |
![]() |
暗恋学妹的小摩托 · 迪士尼“吞下”21世纪福克斯重塑好莱坞格局 | 南方周末 10 月前 |
![]() |
调皮的硬盘 · SQL面试题目汇总 - 知乎 1 年前 |