无法从父 for 循环记录值

用户7332411

JS代码

function generate(rectWidth, rectHeight, amount) {

// holds size
let size = {
    width: [],
    height: []
};

// holds colors
let colors = [];

for (var i = 0; i < amount; i++) {
    // generate size
    var width = Math.floor((Math.random() * rectWidth) + 1);
    var height = Math.floor((Math.random() * rectHeight) + 1);

    var r = Math.floor((Math.random() * 255) + 1);
    var g = Math.floor((Math.random() * 255) + 1);
    var b = Math.floor((Math.random() * 255) + 1);

    // add size to object
    size.width.push(width);
    size.height.push(height);
    colors.push(`rgb(${r}, ${g}, ${b})`);
}

return {
    size: size,
    colors: colors
};

};

let properties = generate(50, 50, 10);

for (let width = 0; width < properties.size.width.length; width++) {
        for (let height = 0; height < properties.size.height; height++) {
            for (let color = 0; color < properties.colors.length; color++) {

                console.log(properties.size.width[width]);

            }
        }
    }

问题

所以我的问题是,在我的代码中,我正在尝试console.log(properties.size.width[width]);,但它没有记录,而且我没有收到错误消息。

我想要发生的事情

我希望我的代码console.log()的值properties.size.width

我试过的

  1. 我试过使用var而不是let在循环内。
LF00

for (let height = 0; height < properties.size.height; height++) 应该

for (let height = 0; height < properties.size.height.length; height++)

function generate(rectWidth, rectHeight, amount) {

// holds size
let size = {
    width: [],
    height: []
};

// holds colors
let colors = [];

for (var i = 0; i < amount; i++) {
    // generate size
    var width = Math.floor((Math.random() * rectWidth) + 1);
    var height = Math.floor((Math.random() * rectHeight) + 1);

    var r = Math.floor((Math.random() * 255) + 1);
    var g = Math.floor((Math.random() * 255) + 1);
    var b = Math.floor((Math.random() * 255) + 1);

    // add size to object
    size.width.push(width);
    size.height.push(height);
    colors.push(`rgb(${r}, ${g}, ${b})`);
}

return {
    size: size,
    colors: colors
};

};

let properties = generate(50, 50, 10);

for (let width = 0; width < properties.size.width.length; width++) {
        for (let height = 0; height < properties.size.height.length; height++) {
            for (let color = 0; color < properties.colors.length; color++) {

                console.log(properties.size.width[width]);

            }
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法在while循环中调用日志记录

来自分类Dev

无法从父目录导入文件

来自分类Dev

MySql仅从父表具有多个值的子表中获取记录

来自分类Dev

无法从父包python导入子级

来自分类Dev

无法从父级继承表格属性

来自分类Dev

无法从父视图调用Fragment的方法

来自分类Dev

Swift无法从父级更新视图

来自分类Dev

无法从父范围访问节点js要求

来自分类Dev

无法从父组件检索TinyMCE的内容

来自分类Dev

无法从父范围找到FormController实例

来自分类Dev

嵌套ListView无法从父ListView获取值

来自分类Dev

无法从父级中的@viewchildren 调用方法

来自分类Dev

扩展类无法从父类获取数据

来自分类Dev

我无法从已经使用GROUP BY的表中循环记录

来自分类Dev

如何从父终端的无限循环中退出?

来自分类Dev

无法从循环为数组分配值

来自分类Dev

无法在For循环中更改字典的值?

来自分类Dev

无法从多维数组循环正确的值

来自分类Dev

无法从循环内的 switch 语句接收值

来自分类Dev

无法在循环外打印相同的值

来自分类Dev

循环遍历特定记录的值列表

来自分类Dev

循环访问 API 请求以记录值

来自分类Dev

SVG不会从父级继承值

来自分类Dev

SVG不会从父级继承值

来自分类Dev

从父级或最近的 Div 获取数据值

来自分类Dev

无法从父级访问功能-从父级向子级发送道具

来自分类Dev

无法将DKIM TXT值设置为DNS记录

来自分类Dev

VBA中的getattribute无法比较记录集中的值

来自分类Dev

从父数组中获取重复值而不是从父数组中获取重复值