![]() |
睿智的打火机 · 千呼万唤,南京地铁7号线终于要全线通车?· 2 周前 · |
![]() |
强健的大白菜 · 崔建春特派员集体会见欧洲国家在港商会负责人· 2 周前 · |
![]() |
爱喝酒的核桃 · 江歌之母江秋莲这5年:凶手服刑了,我为什么还 ...· 7 月前 · |
![]() |
睿智的伤疤 · 分享8个高质量的视频剪辑课程网站· 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),
![]() |
睿智的打火机 · 千呼万唤,南京地铁7号线终于要全线通车? 2 周前 |
![]() |
强健的大白菜 · 崔建春特派员集体会见欧洲国家在港商会负责人 2 周前 |
![]() |
睿智的伤疤 · 分享8个高质量的视频剪辑课程网站 1 年前 |