누군가 ',' 의 경우, specialword" 나타날 수 있는 방법을 말해보게나 탐지합니다 " 어레이에서는? 예:
categories: [
"specialword"
"word1"
"word2"
]
var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turtles") > -1);
>. 힌트 : 지정된 위치를 나타내는 숫자를 indexof 되돌려줍니다 시치발루 발생할 경우, 그것은 결코 처음으로 또는 - 1 >. 테스트타사의
또는
function arrayContains(needle, arrhaystack)
{
return (arrhaystack.indexOf(needle) > -1);
}
[1]: # Browser_compatibility https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/indexof
jQuery 는 '$ 오언라이':
참고로 이나리 되돌려줍니다 색인입니다 http://support. 도왔으매 요소 '0' 는 첫 번째 요소는 어레이입니다. '-' 는 요소점 찾을 수 없습니다.
<! - begin 스니핏: js 숨기십시오: 진정한 콘솔: 진정한 바벨. > 거짓값 -;
var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];
var foundPresent = $.inArray('specialword', categoriesPresent) > -1;
var foundNotPresent = $.inArray('specialword', categoriesNotPresent) > -1;
console.log(foundPresent, foundNotPresent); // true false
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
끝 - < 스니핏 >;!
'$ 오언라이' 는 '하면' 아리스프로토티페이세네쇼프 이를 지원하는 브라우저에 위한 래퍼 (거의 모두야 요즘) 에서 제공하는 한편, 그 심 don& # 39, t. 이는 기본적으로 동등한 '심' 은 더 성어는 / 제시시 아리스프로토티페 추가에는 수 있는 방식으로 업무를 수행하는 것을. 매든 는 [같은 코드] [2]. 요즘 이 옵션을 사용하지 않고 꼭 이래야겠어요 시행하십시오 래퍼 포함한다.
<! - begin 스니핏: js 숨기십시오: 진정한 콘솔: 진정한 바벨. > 거짓값 -;
var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];
var foundPresent = categoriesPresent.indexOf('specialword') > -1;
var foundNotPresent = categoriesNotPresent.indexOf('specialword') > -1;
console.log(foundPresent, foundNotPresent); // true false
끝 - < 스니핏 >;!
아,,, /sub> < < sub> 6.5 금지되나니?!
Javascript 가 '아리스프로토티페스의 크루즈' 이 가장 적합한 옵션에 대해 언급했다.
var found = categories.includes('specialword');
'-' 결과를 비교, 아니 없는 것이다. 우리가 원하는 않니다. 'true' 또는 '거짓' 돌려준다. # 39 의 구형 브라우저를 위한 it& 폴리필이블 [코드를 사용하여 http://support. 매든] [3].
<! - begin 스니핏: js 숨기십시오: 진정한 콘솔: 진정한 바벨. > 거짓값 -;
var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];
var foundPresent = categoriesPresent.includes('specialword');
var foundNotPresent = categoriesNotPresent.includes('specialword');
console.log(foundPresent, foundNotPresent); // true false
끝 - < 스니핏 >;!
[2]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf # 폴리필 [3]: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/includes # 폴리필
Here you go.
$.inArray('specialword', arr)
이 함수는 양의 정수 (이 어레이에는 색인입니다 주어진 값), 또는 '1' 에서 찾을 수 없습니다 경우 주어진 값과 어레이입니다.
이 같은 사용할 수 있어 좋습니다.
if ( $.inArray('specialword', arr) > -1 ) {
// the value is in the array
}
내가 마음에 오언라이 (.) ',' $ # 39, don& it& # 39, s, t, 대부분의 사람들은 이런 추악한 jquery 쯤 솔루션으로 란 wouldn& # 39 않겠다. # 39 는 단순한 '에 포함되어 있는 here& 스니핏 자도으로 (str)' 방식을 너회의 아스널.
$.fn.contains = function (target) {
var result = null;
$(this).each(function (index, item) {
if (item === target) {
result = item;
}
});
return result ? result : false;
}
마찬가지로, 랩 (wrap) '에서' $ 오언라이 확장명으로 수 있습니다.
$.fn.contains = function (target) {
return ($.inArray(target, this) > -1);
}