【问题标题】:How to check the value of an array to another in javascript如何在javascript中检查一个数组的值到另一个数组
【发布时间】:2020-01-23 10:01:55
【问题描述】:

我正在创建地图;我想使用 Array 向每个位置添加一些信息。 例如:我有 3 个 div 和一个具有三个长度的数组(div 的信息)。 现在,每当我将鼠标悬停在 div 上时,它应该将属性 (elID) 与 Array 进行比较,并获取 head 或 Paragraph 等信息。

HTML 示例:

<div  id="container">
        <div elID = "first">first</div>
        <div elID = "second">second</div>
        <div elID = "third">third</div>
</div>

我在 Javascript 中做了什么:

const locations = [
{"location": "first", "head":"This is first the head", "paragraph":"kommtNoch"},
{"location": "second", "head":"This is the second head", "paragraph":"kommtNoch"},
{"location": "third", "head":"This is the third head", "paragraph":"kommtNoch"} ]

const red = document.querySelectorAll('#container > div');

for(i=0; i<red.length;i++){

    red[i].setAttribute('onmouseover', 'myFirstFunction(this)');

    function myFirstFunction(e){

                if(e.classList === locations.indexOf()){
                    console.log(locations[i].location);
                }

我在这里找到了一些答案,但我很难理解:

Check if an array contains any element of another array in JavaScript

【问题讨论】:

    标签: javascript arrays compare


    【解决方案1】:

    我不完全确定您在寻找什么功能,但您可以在每个元素上设置一个事件侦听器来侦听鼠标悬停事件,并获取 elID,您可以使用getAttribute。要在数组中获取正确的对象,可以使用find

    const locations = [
      {"location": "first", "head":"This is first the head", "paragraph":"kommtNoch"},
      {"location": "second", "head":"This is the second head", "paragraph":"kommtNoch"},
      {"location": "third", "head":"This is the third head", "paragraph":"kommtNoch"}
    ]
    
    const red = document.querySelectorAll('#container > div');
    
    const handleMouseOver = e => {
      const o = locations.find(o => e.target.getAttribute('elID') === o.location)
      e.target.innerText = o.head
    }
    
    red.forEach(el => el.addEventListener('mouseover', handleMouseOver))
    <div id="container">
      <div elID="first">first</div>
      <div elID="second">second</div>
      <div elID="third">third</div>
    </div>

    同样,我不确定您要做什么,但听起来您还想添加更多事件侦听器来处理 mouseleave 事件,以恢复更改。

    【讨论】:

    • 你们太棒了。谢谢。你能解释一下你在这里做了什么吗? const handleMouseOver = e => { const o = locations.find(o => e.target.getAttribute('elID') === o.location) e.target.innerText = o.head}
    • 当然。首先,我声明一个匿名函数,这只是function handleMouseOver(e) {} 的一种花哨的写法。在函数内部,我声明了o,它是object 的简写。我使用locations.find,它是数组的内置函数。它循环遍历数组中的每个对象,我将其引用为o。我使用e.target.getAttribute('elID'),它从刚刚“鼠标悬停”的元素中获取elID 值。如果 o.location 与此值相同,则 find 将返回它。在下一行中,我简单地将鼠标悬停的 div 的 innerText 设置为等于返回对象的 head 属性。
    • @hamid 如果你还是不明白,请告诉我:)
    • 谢谢。您能否再解释一下您是如何声明 o 的?我理解了位置。找到但不是在那之后。
    • 当然。您可以找到Array.prototype.findhere 的文档。它循环遍历数组中的每个元素,并执行回调函数。如果回调函数返回 true,它将结束循环并返回该对象。我所做的是编写了另一个匿名函数。它可以重写为function(o) { return e.target.getAttribute('elID') === o.location }o 是对每个循环中数组中对象的引用。你可以随意命名,但o 是我的缩写
    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多