![]() |
痛苦的小蝌蚪 · 口腔种植医疗服务价格专项治理政策问答_市级政 ...· 2 天前 · |
![]() |
想旅行的松球 · 如何产出一份深度价值的产品体验报告(附案例) ...· 9 月前 · |
![]() |
不敢表白的椰子 · 80年前的卡廷森林里,究竟发生了怎样的惨案? ...· 12 月前 · |
![]() |
乐观的洋葱 · 【提醒】年轻女子大腿突然麻木,不到半个月几乎 ...· 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),
![]() |
不敢表白的椰子 · 80年前的卡廷森林里,究竟发生了怎样的惨案?_百科TA说 12 月前 |