猿问

跪求!js数组间循环添加问题?十分感谢

1.元数据constarr1=["#355b54","#12d806","#748aac"];constarr2=[{type:"一类"},{type:"二类"},{type:"三类"},{type:"四类"},{type:"五类"},{type:"六类"},{type:"七类"},{type:"其他"}];
2.需要将arr中数据按照索引插入到arr2,arr1商量继续从arr[0]添加.
3.最后的效果是这样:
constarr=[
{type:"一类",color:"#355b54"},
{type:"二类",color:"#12d806"},
{type:"三类",color:"#748aac"},
{type:"四类",color:"#355b54"},
{type:"五类",color:"#12d806"},
{type:"六类",color:"#748aac"},
{type:"七类",color:"#355b54"},
{type:"其他",color:"#12d806"}
]
动漫人物
浏览 386回答 2
2回答

侃侃无极

constarr=arr2.map((item,index)=>({...item,color:arr1[index%arr1.length]}))

开心每一天1111

letarr1Length=arr1.lengthletresult=arr2.map((item,index)=>{letidx=index%arr1Length//取余,首尾循环return{...item,color:arr1[idx]}})console.log(result)
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答