본문 바로가기

카테고리 없음

[Lodash] _.includes() 함수 설명 및 사용 예제/예시

반응형

구문(Syntax)

_.includes(collection, value, [fromIndex=0])
값이 컬렉션에 있는지 확인합니다. 컬렉션이 문자열이면 값의 서브스트링을 확인하고, 그렇지 않으면 동일성 비교를 위해 SameValueZero가 사용됩니다. FromIndex가 음수이면 컬렉션 종료 후 오프셋으로 사용됩니다.

Checks if value is in collection. If collection is a string, it's checked for a substring of value, otherwise SameValueZero is used for equality comparisons. If fromIndex is negative, it's used as the offset from the end of collection.

 

매개변수(Arguments)

- collection (Array|Object|string): 검사할 컬렉션입니다.
- value (*): 검색할 값입니다.
- [fromIndex=0] (number): 검색할 인덱스입니다.


- collection (Array|Object|string): The collection to inspect.
- value (*): The value to search for.
- [fromIndex=0] (number): The index to search from.

 

반환 값(Returns)

(boolean): 값이 발견되면 true를 반환하고, 그렇지 않으면 false를 반환합니다.

(boolean): Returns true if value is found, else false.

 

 

 

예제(Example)

예제 1) 

_.includes([1, 2, 3], 1);
// [1, 2, 3] 배열을 순회하면서 1인 값이 있으면 true를 리턴합니다.
// 반환 값 => true

예제 2) 

_.includes([1, 2, 3], 1, 2);
// [1, 2, 3] 배열을 순회하면서 인덱스 2에 1인 값이 있으면 true를 리턴합니다.
// 반환 값 => false

 

예제 3) 

_.includes({ 'a': 1, 'b': 2 }, 1);
// { 'a': 1, 'b': 2 } 객체를 순회하면서 1인 값이 있으면 true를 리턴합니다.
// 반환 값 => true

 

예제 4) 

_.includes('abcd', 'bc');
// 'abcd string을 순회하면서 'bc'인 값이 있으면 true를 리턴합니다.
// 반환 값 => true

 

 

 

 

Reference

반응형