简体   繁体   English

使用JQuery.inArray()查找字符串

[英]Find string using JQuery.inArray()

Goal: 1) Create a string using the ID attributes of several elements. 目标: 1)使用几个元素的ID属性创建一个字符串。 2) Push that string to an array. 2)将字符串推入数组。 3) Check to see if the string in the array forms a particular word -- in this case, "idaho". 3)检查数组中的字符串是否构成一个特定的单词-在这种情况下为“ idaho”。

Problem: I can only seem to verify that a particular character is in the array. 问题:我似乎只能验证数组中是否包含特定字符 If I use more than one character in the "value" of "$.inArray", it returns false. 如果在“ $ .inArray”的“值”中使用多个字符,则返回false。

Question: How can I verify that the string in the array forms the word "idaho" instead of just verifying that the string contains an "i" or "d" or "a", etc.? 问题:如何验证数组中的字符串构成单词“ idaho”,而不是仅验证字符串中包含“ i”,“ d”或“ a”等?

This FIDDLE works, but only because it's looking for an "i" instead of "idaho". FIDDLE起作用,但仅是因为它正在寻找“ i”而不是“ idaho”。

I've done a lot of research on this, but can't get it to work. 我对此进行了大量研究,但无法使其正常工作。 Thanks for helping a noob! 感谢您帮助菜鸟!

<div class="pieces" id="i"></div>
<div class="pieces" id="d"></div>
<div class="pieces" id="a"></div>
<div class="pieces" id="h"></div>
<div class="pieces" id="o"></div>

 var piecesArray = [];
 var ids = $('.pieces').map(function() {
   piecesArray.push(this.id);
 });
 piecesArray = piecesArray.join('');
  if ($.inArray('i', piecesArray) !==-1){
    alert(piecesArray);
  }

You can get the current word directly in one go: 您可以一次性获得当前单词:

var pieces = $('.pieces').map(function() {
    return this.id;
}).get().join('');

and then just check that it matches the desired word: 然后只需检查它是否与所需单词匹配即可:

if (pieces === 'idaho') 

You are looking to see if a substring exists within the given string, rather than an element (character) exists in the array (string). 您正在查看给定字符串中是否存在子字符串,而不是数组(字符串)中存在元素(字符)。 The inArray() function searches for a single character in the string. inArray()函数在字符串中搜索单个字符。 Passing a string to the function forces the function to assume the targeted array is an array of strings, which it is not -> returns false. 将字符串传递给该函数会强制该函数假定目标数组是字符串数组,而不是->返回false。

Instead you should use the string.indexOf() function to check to see if the string contains a given substring. 相反,您应该使用string.indexOf()函数检查字符串是否包含给定的子字符串。

if(piecesArray.indexOf('idaho') >= 0) { 
    // idaho is in the string! 
}

If the substring is in the targeted string the function returns the staying index of the substring. 如果子字符串在目标字符串中,则该函数返回子字符串的停留索引。 In this case since the substring is the string it would return 0. 在这种情况下,由于子字符串是字符串,因此它将返回0。

Hope this helps! 希望这可以帮助!

Why don't you just compare your joined array with a string? 为什么不只将连接的数组与字符串进行比较?

var piecesArray = [],
    word;

$(".pieces").each(function() {
    piecesArray.push(this.id);
});

word = piecesArray.join("");

if (word === "idaho"){
    alert(word);
}

Why must you use $.inArray , you joined the pieces into a string, all you need for the alert to work is to replace : 为什么必须使用$.inArray ,将这些片段连接到一个字符串中,所以$.inArray alert正常工作,只需替换:

if ($.inArray('i', piecesArray) !==-1)

with: 有:

if (piecesArray == "idaho")

http://jsfiddle.net/q3x1gg9b/2/ http://jsfiddle.net/q3x1gg9b/2/

Why don't you simple create a string from the start: 为什么不从头开始简单地创建一个字符串:

 var piecesString = "";
      var ids = $('.pieces').map(function() {
            piecesString += this.id;
       });
    if (piecesString == "idaho"){
      alert(piecesString);
    }

Here's a fiddle... http://jsfiddle.net/q3x1gg9b/4/ 这是一个小提琴... http://jsfiddle.net/q3x1gg9b/4/

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

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