JS/JS 공부

[JS] 배열 순환 탐색 메서드

congs 2023. 4. 17. 16:07

array.reduce(callback function ( total, value, index, self ), [init value] ) 

연산의 누적값 리턴

 

array.reduceRight (callback function ( total, value, index, self ), [init value] ) 

배열의 값을 우측에서 좌측방향으로 대입

 

array.every(callback function ( total, value, index, self ))

콜백함수의 리턴 조건에 모든 배열의 값이 만족하면 true / 아니면 false 리턴

- 배열의 and 개념 

 

array.some(callback function ( total, value, index, self ))

콜백함수의 리턴 조건에 만족하는 배열값이 있다면 true / 아니면 false 리턴

 - 배열의 or 개념

 

array.indexOf( "str", [index] )

해당 문자열이 존재하는 배열 index를 리턴

 

array.lastIndexOf( "str", [start index] )

뒤에서부터 검색해 해당 문자열이 존재하는 배열 index 리턴

 

array.find( callback function ( total, value, index, self )) 

검색 조건에 만족하는 첫번째 배열값 리턴

  • ex)  여기에서 __을 찾으시오
  • 검색조건, 값 리턴이 쉬운편이여서 자주 사용

 

array.findIndex(callback function ( total, value, index, self ))

검색조건에 만족하는 첫번째 배열의 index 리턴

 

array.from('객체')

순서가 있거나 순환구조를 가지고 있는 객체를 배열로 변환

- 순서가 없거나 순환구조가 없는 경우 x

 

array.keys()

배열이 사용중인 key(식별자, index)를 검색하여 새로운 배열로 리턴

- 객체에는 적용x

- 배열에만 적용o

 

array.entries()

index:value의 pair 구조의 값을 만들어 새로운 배열에 담아 리턴

- 객체에는 적용x

 

array.includes('찾고 싶은 값')

배열에 찾는 값이 존재한다면 true / 없다면 false

 

 

사용 예시

 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>배열 순환 탐색 메서드</title>
</head>
<body>
    <h1>배열의 순환할 수 있는 구조를 이용하여 특정 기능을 할 수 있는 메서드 제공</h1>

    <script>
        const fruits = ['apple', 'Orange', 'apple', 'banana'];

        // 뒤쪽 apple에 있는 'l' 찾기
        let key = fruits[fruits.indexOf('apple',1)]; //1번지부터 검색해 나온 apple
        console.log(key);
        // let key1 = fruits[fruits.indexOf('apple',1)].indexOf('l'); //1번지부터 검색해 나온 apple의 l의 위치: 3
        let key1 = fruits[fruits.indexOf('apple',1)].indexOf('app')+'app'.length;
        console.log(key1);

        const f = fruits.entries();
        console.log(f); // 보지 못함
        for(const g of f){
            console.log(g);
            console.log(typeof g);
        };
    </script>
</body>
</html>