투케이2K

135. (javascript/자바스크립트) lodash 라이브러리 - findIndex 사용해 특정 json key 값을 만족하는 인덱스 번지 값 출력 실시 본문

JavaScript

135. (javascript/자바스크립트) lodash 라이브러리 - findIndex 사용해 특정 json key 값을 만족하는 인덱스 번지 값 출력 실시

투케이2K 2022. 5. 28. 18:10
반응형

[개발 환경 설정]

개발 툴 : Edit++

개발 언어 : javascript

 

[소스 코드]

    <!-- [lodash 라이브러리 설치] -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>



    <!-- [내부 자바스크립트 J쿼리 이벤트 지정] -->
    <script>


        /* [html 최초 로드 및 이벤트 상시 대기 실시] */
        window.onload = function() {
            console.log("");
            console.log("[window onload] : [start]");
            console.log(""); 


            // [테스트 함수 호출]
            testMain();
        };



        /* [자바스크립트 테스트 코드] */
        function testMain(){
            console.log("");
            console.log("[testMain] : [start]");
            console.log("");


            /*
            [요약 설명]
            1. Lodash 는 객체, 배열 등의 데이터의 구조를 쉽게 사용할 수 있게해주는 자바스크립트 라이브러리입니다
            2. Lodash 설치 및 CDN 참고 사이트 : https://cdnjs.com/libraries/lodash.js
            3. Lodash 문법 참고 사이트 : https://lodash.com/docs/4.17.15
            4. findIndex : 배열에서 특정 값을 찾는 인덱스 번지 값을 반환합니다
            */


            // [초기 변수 선언 실시]
            var array = [
                {"index" : 0, "name" : "twok"},
                {"index" : 1, "name" : "투케이"}
            ];

            console.log("");
            console.log("원본 : " + JSON.stringify(array));
            console.log("");


            // [findIndex 사용해 찾으려는 특정 key 값 인덱스 번지 확인]
            var name_1 = _.findIndex(array, ['name', "투케이"]); // [특정 key 값 포함 번지 확인]
            var name_2 = _.findIndex(array, {"index" : 1, "name" : "투케이"}); // [특정 값 포함 번지 확인]

            console.log("");
            console.log("name_1 : " + name_1);
            console.log("name_2 : " + name_2);
            console.log("");
        };

    </script>
 

[결과 출력]


반응형
Comments