js 每两个图片插入一个广告怎么做?

要求每两张图片后插入一个广告,请问这怎么做,有偿,微信 jinnchen94
image.png

阅读 2.1k
3 个回答
var arr = [1,2,3,4,5,6]
for(let i = 2; i < arr.length; i+=2){
    arr.splice(i++, 0, '') // 这里空字符串换成你的广告
}

arr // [1, 2, "", 3, 4, "", 5, 6]
新手上路,请多包涵
    setAdCustomZwy(items, pageNum) {
        if (!不符合条件) {
            return items;
        }
        let start;
        let space = this.config.space - 1;
        if (pageNum == 1) {
            start = this.config.firstAd - 1
        } else {
            start = this.isStart;
        }
        while (items.length > start) {
            items.splice(start, 0, {
                type: 'adCustom',
                title: 'adCustom999',
                videoId: 0
            })
            start = space + start + 1;
        }
        this.isStart = start - items.length;
        return items;
    },
var arr = [...Array(10).keys()];//生成0-9
//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var obj = 'ad';//要添加的数据
arr.reduce((acc,cur,idx)=>(acc.push(cur),idx%2 && acc.push(obj),acc),[]);
//[0, 1, "ad", 2, 3, "ad", 4, 5, "ad", 6, 7, "ad", 8, 9, "ad"]
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题