我正在尝试根据半径和x,y坐标制作一个圆。我已经完成了所有工作,除了数组不是我使用的正确格式。 我得到:
[
"X_PROPS:40,Y_PROPS:0",
"X_PROPS:39.99390780625565,Y_PROPS:0.6980962574913405",
"X_PROPS:39.97563308076383,Y_PROPS:1.3959798681000388",
"X_PROPS:39.94518139018295,Y_PROPS:2.093438249717753"
]
但我需要:
[
{X_PROPS:40,Y_PROPS:0},
{X_PROPS:39.99390780625565,Y_PROPS:0.6980962574913405},
{X_PROPS:39.97563308076383,Y_PROPS:1.3959798681000388},
{X_PROPS:39.94518139018295,Y_PROPS:2.093438249717753}
]
我尝试过:
function spec(radius, steps, centerX, centerY){
var xValues = [centerX];
var yValues = [centerY];
var result = [];
for (var i = 0; i < steps; i++) {
xValues[i] = (centerX + radius * Math.cos(2 * Math.PI * i / steps));
yValues[i] = (centerY + radius * Math.sin(2 * Math.PI * i / steps));
result.push('X_PROPS:'+ xValues[i]+','+'Y_PROPS:'+ yValues[i]);
}
return result;
}
console.log(spec(40,360,0,0))
答案 0 :(得分:5)
此表达式'X_PROPS:'+ xValues[i]+','+'Y_PROPS:'+ yValues[i]
创建一个字符串。而是创建对象文字:
function spec(radius, steps, centerX, centerY) {
var result = [];
for (var i = 0; i < steps; i++) {
result.push({
X_PROPS: (centerX + radius * Math.cos(2 * Math.PI * i / steps)),
Y_PROPS: (centerY + radius * Math.sin(2 * Math.PI * i / steps))
});
}
return result;
}
console.log(spec(40, 360, 0, 0))