Arrays 反应:如何从API对象创建数组,然后随机选择一个元素作为值?
你们知道我如何制作一个x,y坐标的数组,然后随机选择一个来放入一个值中吗Arrays 反应:如何从API对象创建数组,然后随机选择一个元素作为值?,arrays,api,reactjs,jsx,Arrays,Api,Reactjs,Jsx,你们知道我如何制作一个x,y坐标的数组,然后随机选择一个来放入一个值中吗 <g id="lines"> {data.sites.map((item, i) => { let findX = (180 + item.attributes.address.Longitude) * (1552 / 360); let findY = (90 - item.attributes.address.Latitude) * (818/ 180); let randoXY =
<g id="lines">
{data.sites.map((item, i) => {
let findX = (180 + item.attributes.address.Longitude) * (1552 / 360);
let findY = (90 - item.attributes.address.Latitude) * (818/ 180);
let randoXY = findX + ',' findY (and randomly selects one of the X, Y values);
return (
<path key={i + i + '--'} id={'line' + (i + 1)} class="cls-3" d={'M' + findX + ','
+ findY +'L' + randoXY} />
)
})}
</g>
{data.sites.map((项目,i)=>{
设findX=(180+item.attributes.address.Longitude)*(1552/360);
让findY=(90-item.attributes.address.Latitude)*(818/180);
让randoXY=findX+','findY(并随机选择其中一个X,Y值);
返回(
)
})}
创建一个包含一些随机点的数组
:
let arr = [0,1,2,3,4,5,6,7,8,9];
然后使用Math.random()
选择一个随机索引,确保索引始终位于数组的长度范围内(始终将Math.random()
的值乘以数组的长度,这样它就永远不会大于长度),然后使用Math.floor()
将值转换为整数
使用此选项从阵列中选择随机x和y值:
<g id="lines">
{data.sites.map((item, i) => {
let findX = (180 + item.attributes.address.Longitude) * (1552 / 360);
let findY = (90 - item.attributes.address.Latitude) * (818/ 180);
let x = a[Math.floor(Math.random() * 10)];
let y = a[Math.floor(Math.random() * 10)];
return (
<path
key={i + i + '--'}
id={'line' + (i + 1)}
class="cls-3"
d={`M${findX}, ${findY}L${x},${y}`} />
)})
}
</g>
{data.sites.map((项目,i)=>{
设findX=(180+item.attributes.address.Longitude)*(1552/360);
让findY=(90-item.attributes.address.Latitude)*(818/180);
设x=a[Math.floor(Math.random()*10)];
设y=a[Math.floor(Math.random()*10)];
返回(
)})
}