搜索许多数组以查看是否有任何名称与另一个var匹配? [英] Searching through many arrays to see if any of the name matches another var?

查看:63
本文介绍了搜索许多数组以查看是否有任何名称与另一个var匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试创建一个函数,如果图像数组的名称与所选元素的data属性匹配,则会创建一个警报.在我继续执行我的实际计划之前,这只是一个测试,但是我被困在这里.我的困惑是双重的.

Hey I'm trying to make a function that creates an alert if the name of an array of images matches the data attribute of the selected element. This is simply a test before I move forward with my actual plans but I'm stuck here. My confusion is twofold.

注意:抱歉,我是javascript新手,请多多包涵.

Note: Sorry, I am new to javascript, please bear with me.

1)如何将数组名称与var进行比较?

1)How do I compare the name of an array with a var?

2)如何搜索存在的所有数组,我是否需要创建嵌套数组,或为此使用其他函数? idk

2)How do I search through all of the arrays present, do I need to make nested arrays, or another function for that? idk

我的网页上有数量不确定的模态图像,单击这些模态图像可打开图像的幻灯片相册.我得到了适用于1张图片的图像,然后意识到要使其用于数量不确定的幻灯片放映,我应该制作一个函数来填充幻灯片div.我计划让每个模态图像的数据属性都为"1,2,3 ... etc",并拥有一堆数组,每个图像的名称都类似地命名为"1,2,3 ... etc",如果两者的名称匹配,然后它将用数组中的图像填充幻灯片.这样,如果我需要添加另一个模式图像,我需要做的就是给它适当的ID并添加其图像数组.我希望这一点很清楚,否则请提供更多说明.

My webpage have an undetermined amount of modal images which , when clicked, open a slideshow album of images. I got this working for 1 image then realized that to have it work for an undetermined amount of slideshows of undetermined size I should make a function that fills the slideshow div. I planned to have each modal image to have a data attribute of "1,2,3...etc" and have a bunch of arrays with the images each named similarly "1,2,3...etc" and if the names of both match up, then it will populate the slideshow with the images in the array. This way If i need to add another modal image all i need to do is give it the appropriate id and add an array of its images. I hope this was clear, if not then I will provide more clarification.

HTML:

<div class="row">
  <div class="column">
    <img id="modal-1" src="https://www.yosemitehikes.com/images/wallpaper/yosemitehikes.com-bridalveil-winter-1200x800.jpg" style="max-width:100%" data-modal="1" onclick="/*openModal();currentSlide(1);*/ fillSlides(this);" class="hover-shadow cursor">
  </div>
</div>

Javascript:

Javascript:

 function fillSlides(modalID){
    var slides_1 = ["Images/LS_01.jpg", "Images/LS_02.jpg", "Images/LS_03.jpg", "Images/LS_04.jpg" ];
 var slides_2 = ["Images/LS_05.jpg", "Images/LS_06.jpg", "Images/LS_07.jpg", "Images/LS_08.jpg" ];
var slides_3 = ["Images/LS_09.jpg", "Images/LS_10.jpg", "Images/LS_11.jpg", "Images/LS_12.jpg" ];

    var modal_num = modalID.getAttribute('data-modal');
    alert(modal_num);

    }

推荐答案

您应该使用将所需数据映射到图像数组的对象:

You should use an object that maps the data you want to the array of images:

var slides = {
    "1": ["Images/LS_01.jpg", "Images/LS_02.jpg", "Images/LS_03.jpg", "Images/LS_04.jpg" ],
    "2": ["Images/LS_05.jpg", "Images/LS_06.jpg", "Images/LS_07.jpg", "Images/LS_08.jpg" ],
    "3": ["Images/LS_09.jpg", "Images/LS_10.jpg", "Images/LS_11.jpg", "Images/LS_12.jpg" ]
};

然后,您可以使用slides[modal_num]来获取所单击图像的幻灯片.

Then you can use slides[modal_num] to get the slides for the images that was clicked.

这篇关于搜索许多数组以查看是否有任何名称与另一个var匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆