복붙노트

[JQUERY] 배열 자바 스크립트 / jQuery를 특정 문자열을 포함하는 경우 어떻게 찾아 낼 수 있을까요? [복제]

JQUERY

배열 자바 스크립트 / jQuery를 특정 문자열을 포함하는 경우 어떻게 찾아 낼 수 있을까요? [복제]

해결법


  1. 1.당신은 정말 이것에 대한 jQuery를 필요가 없습니다.

    당신은 정말 이것에 대한 jQuery를 필요가 없습니다.

    var myarr = ["I", "like", "turtles"];
    var arraycontainsturtles = (myarr.indexOf("turtles") > -1);
    

    또는

    function arrayContains(needle, arrhaystack)
    {
        return (arrhaystack.indexOf(needle) > -1);
    }
    

    사항 Array.indexOf (..)가 IE <9에서 지원되지 않는다는 지적이의 가치가 있지만, jQuery의 같이 IndexOf (...) 함수는 심지어 이전 버전에 대한 작동합니다.


  2. 2.jQuery를이 $ .inArray을 제공합니다 :

    jQuery를이 $ .inArray을 제공합니다 :

    inArray 0 개 원소를 나타내고 있으므로, 소자의 인덱스가 찾아 반환 참고 배열에서 첫 번째이다. -1 요소는 발견되지 않았다 나타낸다.

    VAR categoriesPresent = '워드', '워드', 'specialword', '워드']; VAR categoriesNotPresent = '워드', '워드', '워드']; VAR foundPresent = $ .inArray ( 'specialword'categoriesPresent)> -1; VAR foundNotPresent = $ .inArray ( 'specialword'categoriesNotPresent)> -1; CONSOLE.LOG (foundPresent, foundNotPresent); // 허위 사실 <스크립트 SRC = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">

    편집 3.5 년 후

    그렇지 않은에서 심 (shim)을 제공하면서 $ .inArray 효과적으로 (거의 모두 요즘)를 지원하는 브라우저에서 Array.prototype.indexOf에 대한 래퍼입니다. 그것은 일을 더 관용적 / JSish 방법입니다 Array.prototype으로에 심을 추가, 본질적으로 동일합니다. MDN은 코드를 제공합니다. 요즘 차라리 jQuery를 래퍼를 사용하는 것보다,이 옵션을 걸릴 것이다.

    VAR categoriesPresent = '워드', '워드', 'specialword', '워드']; VAR categoriesNotPresent = '워드', '워드', '워드']; VAR foundPresent categoriesPresent.indexOf = ( 'specialword')> -1; VAR foundNotPresent categoriesNotPresent.indexOf = ( 'specialword')> -1; CONSOLE.LOG (foundPresent, foundNotPresent); // 허위 사실

    편집 다른 삼년 후

    아이쿠, 6.5 년!

    현대 자바 스크립트에서이를위한 최선의 선택은 Array.prototype.includes입니다 :

    var found = categories.includes('specialword');
    

    어떤 비교하지없이 혼란 -1 결과. 그것은 우리가 원하는 것을 : 그것은 true 또는 false를 반환합니다. 이전 버전의 브라우저를 위해 그것은 MDN의 코드를 사용하여 polyfillable입니다.

    VAR categoriesPresent = '워드', '워드', 'specialword', '워드']; VAR categoriesNotPresent = '워드', '워드', '워드']; VAR foundPresent categoriesPresent.includes = ( 'specialword'); VAR foundNotPresent categoriesNotPresent.includes = ( 'specialword'); CONSOLE.LOG (foundPresent, foundNotPresent); // 허위 사실


  3. 3.여기 가서 :

    여기 가서 :

    $.inArray('specialword', arr)
    

    이 함수는 양의 정수 (지정된 값의 배열 인덱스)를 반환 -1 주어진 값 어레이에서 발견되지 않은 경우.

    라이브 데모 : http://jsfiddle.net/simevidas/5Gdfc/

    당신은 아마과 같이이를 사용하려면 :

    if ( $.inArray('specialword', arr) > -1 ) {
        // the value is in the array
    }
    

  4. 4.당신은 루프를 사용할 수 있습니다 :

    당신은 루프를 사용할 수 있습니다 :

    var found = false;
    for (var i = 0; i < categories.length && !found; i++) {
      if (categories[i] === "specialword") {
        found = true;
        break;
      }
    }
    

  5. 5.나는 $ .inArray (..) 좋아하지 않아, 대부분의 제정신 사람들이 용납하지 않을 것 못생긴, jQuery를 흉내 솔루션의 종류입니다. 다음은 간단한이 당신의 무기고에 (STR) 메소드를 포함 추가하는 코드 조각입니다 :

    나는 $ .inArray (..) 좋아하지 않아, 대부분의 제정신 사람들이 용납하지 않을 것 못생긴, jQuery를 흉내 솔루션의 종류입니다. 다음은 간단한이 당신의 무기고에 (STR) 메소드를 포함 추가하는 코드 조각입니다 :

    $.fn.contains = function (target) {
      var result = null;
      $(this).each(function (index, item) {
        if (item === target) {
          result = item;
        }
      });
      return result ? result : false;
    }
    

    마찬가지로 확장에 $ .inArray 포장 수 :

    $.fn.contains = function (target) {
      return ($.inArray(target, this) > -1);
    }
    
  6. from https://stackoverflow.com/questions/6116474/how-to-find-if-an-array-contains-a-specific-string-in-javascript-jquery by cc-by-sa and MIT license