Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果在javascript中有值,则禁用数组_Javascript_Html_Ads_Shuffle_Banner - Fatal编程技术网

如果在javascript中有值,则禁用数组

如果在javascript中有值,则禁用数组,javascript,html,ads,shuffle,banner,Javascript,Html,Ads,Shuffle,Banner,我正在尝试用javascript开发一个简单的横幅旋转 如何确保洗牌中只包含值为“active”:1的数组 <div id="ad-container"></div> <script> var banner = [{ "img": "https://DOMAIN.TLD/IMG.JPG", "url": "https://LINK.DOMAIN.TLD", "maxWidth": 468, "ac

我正在尝试用javascript开发一个简单的横幅旋转

如何确保洗牌中只包含值为“active”:1的数组

<div id="ad-container"></div>
<script>
var banner = [{
        "img": "https://DOMAIN.TLD/IMG.JPG",
        "url": "https://LINK.DOMAIN.TLD",
        "maxWidth": 468,
        "active": 1
      },
      {
        "img": "https://DOMAIN.TLD/IMG2.JPG",
        "url": "https://LINK2.DOMAIN.TLD",
        "maxWidth": 468,
        "active": 0
      }
,
      {
        "img": "https://DOMAIN.TLD/IMG3.JPG",
        "url": "https://LINK3.DOMAIN.TLD",
        "maxWidth": 468,
        "active": 1
      }
    ];

function shuffle(banners) {
      var j, x, i;
      for (i = banners.length - 1; i > 0; i--) {

        j = Math.floor(Math.random() * (i + 1));
        x = banners[i];
        banners[i] = banners[j];
        banners[j] = x;
      }
      return banners;
    }

    shuffle(banner);

document.getElementById('ad-container').innerHTML = '<a href="' + banner[0]["url"] + '"><img src="' + banner[0]["img"] + '" style="width: 100%;height: auto;max-width: ' + banner[0]["maxWidth"] + 'px;"></a>';

</script>

var横幅=[{
“img”:https://DOMAIN.TLD/IMG.JPG",
“url”:”https://LINK.DOMAIN.TLD",
“最大宽度”:468,
“活动”:1
},
{
“img”:https://DOMAIN.TLD/IMG2.JPG",
“url”:”https://LINK2.DOMAIN.TLD",
“最大宽度”:468,
“活动”:0
}
,
{
“img”:https://DOMAIN.TLD/IMG3.JPG",
“url”:”https://LINK3.DOMAIN.TLD",
“最大宽度”:468,
“活动”:1
}
];
功能洗牌(横幅){
变量j,x,i;
对于(i=banner.length-1;i>0;i--){
j=数学地板(数学随机()*(i+1));
x=横幅[i];
横幅[i]=横幅[j];
横幅[j]=x;
}
返回横幅;
}
洗牌(横幅);
document.getElementById('ad-container')。innerHTML='';
值为“active”:0的所有数组不应包括在随机播放中

也许有人知道如何改进shuffle/代码,或者如果同一页上有更多document.getElementById包含,如何防止同一横幅在同一页上显示多次

提前感谢。

只需过滤阵列即可

var横幅=[{
“img”:https://DOMAIN.TLD/IMG.JPG",
“url”:”https://LINK.DOMAIN.TLD",
“最大宽度”:468,
“活动”:1
},
{
“img”:https://DOMAIN.TLD/IMG2.JPG",
“url”:”https://LINK2.DOMAIN.TLD",
“最大宽度”:468,
“活动”:0
}
,
{
“img”:https://DOMAIN.TLD/IMG3.JPG",
“url”:”https://LINK3.DOMAIN.TLD",
“最大宽度”:468,
“活动”:1
}
];
让activeParts=banner.filter(info=>info.active);
console.log(activeParts);
//做任何事,例如:
//洗牌(活动部分)
这将缩小阵列,使其仅包含阵列中的活动对象


另一方面,在maxwidth映射变异值并返回与这些值长度相同的数组后,json缺少一些逗号。因此,这不会缩小您的阵列。我认为您打算链接到
array.map
文档:
banner = banner.map( value => {
   if( value.active === 1 ){
      return value
   }
});