在JavaScript中使用for循环创建对象数组 | 您所在的位置:网站首页 › 循环生成数组 › 在JavaScript中使用for循环创建对象数组 |
我想使用另一个数组中的值通过 for 循环创建一个对象数组。 下面的代码片段生成5值,而不是6 (根据需要) 代码语言:javascript复制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 数组中的值一样多的值? |
CopyRight 2018-2019 实验室设备网 版权所有 |