简体   繁体   English

在JavaScript中的数组中搜索

[英]search within an array in javascript

I've been trying for a while now to search within an array, I've looked at all the other questions that even somewhat resemble mine and nothing works, so I'm asking for any help you can give now.. 我已经尝试了一段时间以在数组中搜索,我查看了所有其他问题,这些问题甚至有些像我的,但没有任何效果,因此,我希望您现在能提供任何帮助。

I have an array with a more complex insides than a simple string array 我有一个比简单的字符串数组更复杂的内部数组

    var elementDefns = [
    {"element":"water", "combos": {"air":"steam", "earth":"sand"} },
    {"element":"fire", "combos": {"earth":"lava", "air":"energy"} },
    {"element":"air", "combos": {"water":"steam", "earth":"dust"} },
    {"element":"earth", "combos": {"water":"swamp", "fire":"lava"} },
];

Two elements are picked (by the users) which are combined to create new elements. (用户)选择了两个元素,将它们组合在一起以创建新元素。 I'd like to search through the elements for any combos that can be made. 我想在元素中搜索可以进行的任何组合。 Ideally, I'd want to use Array.prototype.find, although I can't figure out how to use polyfills correctly and i'm unsure if i'm writing it correctly, so it continues to not work 理想情况下,我想使用Array.prototype.find,尽管我无法弄清楚如何正确使用polyfill,而且我不确定是否正确编写了它,因此它仍然无法正常工作

    var elementOne = $("#board img:first-child").attr('id'); 
    var elementTwo = $("#board img:last-child").attr('id');

    function findElement(element) { 
        return elementDefns.element === elementOne;
    }

board is the id div where the element cards go to once clicked. 木板是ID div,单击该元素卡即可。 I also tried a loop 我也尝试过循环

    for (var i=0,  tot=elementDefns.length; i < tot; i++) {
            var indexHelp = elementDefns[i].element;
            var find = indexHelp.search(elementOne);
            console.log(find);
        }

I'm trying to post a question that's not too long, but I'm sure there's lots more about my code i need to adjust in order to do this. 我正在尝试发布一个不太长的问题,但是我敢肯定,为了做到这一点,我需要对我的代码进行更多调整。 I guess I'm just asking if there's something obvious you think i could work on. 我想我只是问是否有明显的事情您认为我可以继续进行。 I've looked at most of the answers on this site to similar problems but its all just going horribly wrong so any other support would be greatly appreciated.. 我已经看过了本网站上类似问题的大多数答案,但它们都完全出错了,因此,我们将不胜感激任何其他支持。

I have an array with a more complex insides than a simple string array 我有一个比简单的字符串数组更复杂的内部数组

Yes, but why? 是的,但是为什么? Get rid of the extra layers and this is trivial 摆脱多余的图层,这是微不足道的

var e1 = "water";
var e2 = "air";

var elementDefns = {
    "water": {"combos": {"air":"steam", "earth":"sand"} },
    "fire":  {"combos": {"earth":"lava", "air":"energy"} },
    "air":   {"combos": {"water":"steam", "earth":"dust"} },
    "earth": {"combos": {"water":"swamp", "fire":"lava"} },
};

elementDefns[e1].combos[e2] = > "steam" elementDefns [e1] .combos [e2] =>“ steam”

If you want to keep your data-structure, you can filter through it like this: 如果要保留数据结构,可以像这样过滤它:

  var matches = elementDefns
      .filter(e => e.element == first && e.combos[second] !== null)
      .map(e => e.combos[second]);

The first row filters out all matches, and the secon maps it over to the actual match-string (element name). 第一行过滤掉所有匹配项,secon将其映射到实际的匹配字符串(元素名称)。 The find() you speak of just returns the first value that matches, and i guess you want all , so that would be the filter() method. 您所说的find()只是返回匹配的第一个值,我想您想要all ,所以将是filter()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM