数组遍历循环问题

var val = [

    'aa','bb','cc','|',    'dd','ee','ff','|'
    ];

//...理想输出的数据格式为以 '|'分割为一行,向下循环

<p>

    <span>aa</span>
    <span>bb</span>
    <span>cc</span></p><p>
    <span>dd</span>
    <span>ee</span>
    <span>ff</span></p>

思路乱了


临摹微笑
浏览 506回答 1
1回答

小怪兽爱吃肉

var arr=['aa','bb','cc','|', 'dd','ee','ff','|'];var html=arr.reduce(function (str, item) {&nbsp; &nbsp; return item=='|'?str+item:str+'<span>'+item+'</span>'},'');html=html.split('|').map(function (item) {&nbsp; &nbsp; return item?'<p>'+item+'</p>':'';});html=html.join('');console.log(html);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript