• 회원가입
  • 로그인
  • 구글아이디로 로그인

[Array] JS - some() 메서드 - 일부배열값 함수충족여부 체크 (= some메서드 = 썸메서드/섬메서드) ※ 사용자정의필터 통과하는 배열값 존재 여부 체크

목차

  1. array.some() 예제 - 일부 배열값이 지정함수 충족 하는지 체크
  2. array.some() 정의
  3. array.some() 구문
  4. array.some() 예제 - 일부 배열값의 특정 부분이 동일한지 체크
  5. array.some() 예제 - 일부 배열값이 입력값보다 큰지 체크

 

array.some() 예제 - 일부 배열값이 지정함수 충족 하는지 체크 

 

<button onclick="homzzang()">클릭</button>


<p id="demo"></p>


<script>

var nums = [5, 10, 15, 20];


function checkNum(num) {

  return num >= 15;

}


function homzzang() {

  document.getElementById("demo").innerHTML = nums.some(checkNum);

}

</script>


결과값: true

 

array.some() 정의

 

일부 배열요소가 지정함수 충족하는지 여부 체크.

 


 

1.

  • 하나라도 충족 시, true 반환. 
  • 하나도 충족 안 하면, false 반환.
  • 빈 요소에 대해선 함수 실행 X
  • 원본 배열을 변경 X

 

2. cf.

array.every() 메서드 - 모든 배열요소가 지정함수 충족하는지 체크.

 

3.

  • ECMAScript3 (ES3 : JavaScript 1999)
  • 모든 브라우저 지원.

 

4. MDN some() 예제보기

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

 

 

array.some() 구문

 

array.some(function(currentValue, index, arr), thisValue)

 


[매개변수]

 

function(currentValue, index, arr)

필수. 배열의 각 요소에 대해 실행할 함수.

  • currentValue - 현재요소의 배열값
  • index - 현재요소의 색인번호
  • arr - 현재요소가 속한 배열

 

thisValue

선택. 함수에 this 값으로 전달되는 값. (기본값: undefined)

 


[반환값]

 

  • 일부 배열요소가 함수 통과 시, true 반환.
  • 모든 요소가 함수 통과 실패 시, false 반환.

  

 

array.some() 예제 - 일부 배열값의 특정 부분이 동일한지 체크

(예) 일부 언어가 같은 레벨인지 체크.

 

<p id="demo"></p>


<script>

const langs = [

  { name: "HTML", level: 1},

  { name: "CSS", level: 1},

  { name: "JS",   level: 2},

  { name: "PHP",  level: 3}

];


document.getElementById("demo").innerHTML = langs.some(isSameLevel);


function isSameLevel(value,key,arr) {

  if (key === 0){ // 맨처음값 반환

    return true;

  } else { // 이전값과 비교

    return (value.level === arr[key - 1].level);

  }

}

</script>


결과값: true


array.some() 예제 - 일부 배열값이 입력값보다 큰지 체크

 

<p><input type="number" id="inputNum" value="2"></p>


<button onclick="homzzang()">클릭</button>


<p id="demo"></p>


<script>

const nums = [4, 5, 3, 6];


function checkNum(num) {

  return num > document.getElementById("inputNum").value;

}


function homzzang() {

  document.getElementById("demo").innerHTML = nums.some(checkNum);

}

</script>

 

결과값: 사용자 입력값에 따라 (true/false) 반환.



분류 제목
Basic JS - Let 키워드 ★★★★★ - 변수 블럭범위 설정 (렛키워드) ※ 클로저
Modernizr JS - Modernizr (모더나이저 = 마더나이저) - 크로스브라우징 구현 (= 브라우저의 HTML5 C…
React JS - React (리액트) - JS 라이브러리 일종으로 사용자 인터페이스 구축에 사용.(= 현재시간 = …
DOM_Attribute JS - Attribute 객체 - 속성객체
Window_Console JS - window.console 객체 - 콘솔객체
DOM_Document JS - Document 객체 - 웹문서 객체의 속성/메서드 종류 (= document객체 = 다큐먼트객체)
DOM_Element JS - Element 객체 - 요소객체 (속성 + 메서드) 종류
DOM_Event JS - Event 종류 ★ - 이벤트부착 / 이벤트종류 / 이벤트속성 / 이벤트메서드
API_Geolocation JS - Geolocation - 지리위치객체종류
Window_History JS - window.history 객체 정의 + 히스토리 객체의 (속성/메서드) 종류
DOM_HTMLCollection JS - HTMLCollection 객체 (속성 + 메서드) 종류
Window_Location JS - window.location 객체 - 위치 객체 (= window.location객체 = 윈도우로케…
Window_Navigator JS - window.navigator 객체 - 네비게이터 객체 (= window.navigator객체 = …
Window_Screen JS - window.screen 객체 - 스크린 객체 (= window.screen객체 = 윈도우스크린객체…
DOM_Style JS - CSS 스타일 속성값 반환 ★★ + style 객체의 속성 종류
Window_Object JS - window 객체 - 윈도우객체 (속성/메서드) 종류
API_Storage JS - Storage Object - 스토리지 객체 (※ 데이터 저장소)
Object JS - Object Constructor - 객체생성자종류 ★
Object JS - Object Prototypes - 객체프로토타입 (= 객체원형) ★
Object JS - object ECMAScript 5 - 객체혁명
22/67
목록
찾아주셔서 감사합니다. Since 2012