gpt4 book ai didi

javascript - 函数不返回值

转载 作者:行者123 更新时间:2023-11-30 14:24:46 26 4
gpt4 key购买 nike

我正在尝试生成随机值,这是我的代码:

function generateData(total) {
var total = total;
var names = ['Antonie Lereno', 'Laura Saucini', 'Marco Mendez Ortega', 'Lucas Simon Jainte', 'Angel Rodriguez', 'Manuel Salgado', 'Rosario Parrales'];
var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];

return {
labels: months,
datasets: (function () {
var ret = [];

for (var i = 0; i < total; i++) {
var background = [Math.round(Math.random() * 254), Math.round(Math.random() * 254), Math.round(Math.random() * 254)];

ret[i] = {
label: names[Math.round(Math.random() * names.length)],
data: (function () {
var dat = [];

for (var j = 0; j < months.length; j++) {
dat[j] = Math.round(Math.random() * 50);
}

return dat;
}),
backgroundColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',0.2)',
borderColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',1)',
borderWidth: 1
};
}

return ret;
})
};
}

当我调试函数时,例如在 console.log(generateData(1));它仅显示在数组“标签”中,但“数据集”为空。有什么帮助吗?谢谢。

最佳答案

您应该执行 IIFE。在该函数之后添加一组括号。

datasets: (function () {
var ret = [];

for (var i = 0; i < total; i++) {
var background = [Math.round(Math.random() * 254), Math.round(Math.random() * 254), Math.round(Math.random() * 254)];

ret[i] = {
label: names[Math.round(Math.random() * names.length)],
data: (function () {
var dat = [];

for (var j = 0; j < months.length; j++) {
dat[j] = Math.round(Math.random() * 50);
}

return dat;
}),
backgroundColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',0.2)',
borderColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',1)',
borderWidth: 1
};
}

return ret;
})() // <----------------------- Look here

工作片段

而且你不需要设置 var total = total; 而是看下面的方式:

function generateData(total) {
var names = ['Antonie Lereno', 'Laura Saucini', 'Marco Mendez Ortega', 'Lucas Simon Jainte', 'Angel Rodriguez', 'Manuel Salgado', 'Rosario Parrales'];
var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];

return {
labels: months,
datasets: (function() {
var ret = [];

for (var i = 0; i < total; i++) {
var background = [Math.round(Math.random() * 254), Math.round(Math.random() * 254), Math.round(Math.random() * 254)];

ret[i] = {
label: names[Math.round(Math.random() * names.length)],
data: (function() {
var dat = [];

for (var j = 0; j < months.length; j++) {
dat[j] = Math.round(Math.random() * 50);
}

return dat;
}),
backgroundColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',0.2)',
borderColor: 'rgba(' + background[0] + ',' + background[1] + ',' + background[2] + ',1)',
borderWidth: 1
};
}

return ret;
})()
};
}
console.log(generateData(5));

关于javascript - 函数不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52118102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com