728x90

array 3

javascript 에서 여러 배열을 병합하는 방법

javascript 에서 여러 배열을 병합 const itemArray = [ { item: [ '1', '2' ], }, { item: [ '3' ], }, { item: [ '4' ], } ]; // foreach 사용 let itemCollection = []; itemArray.forEach((ele) => { itemCollection = [...itemCollection, ...ele.item]; }); // map 사용 let itemCollection = []; itemArray.map((ele) => { itemCollection = [...itemCollection, ...ele.item]; }); console.log(itemCollection); 결과 ['1', '2', '3', '..

개발/javascript 2022.12.12

자바스크립트 코딩 테스트 몇가지(5) - 응용

1. 활성화된 값의 개수 가져오기 문제 : object 에 담긴 값중 특정 키값을 제외한 나머지중 value가 true인 속성의 개수를 리턴합니다. * 입력 : obj = { option1 : false, option2 : true, option3 : false, option4 : true, option5 : false, option6: false } exception = 'option2, option4'; * 출력 : 0 function solution(obj, exception) { var answer = 0; for (const prop in obj) { if (exception.indexOf(prop) === -1) { if (obj[prop]) { answer++; } } } return ans..

개발/javascript 2022.05.25

자바스크립트 배열 내장 함수(2)

1) findIndex : 주어진 조건의 함수를 만족하는 배열의 첫 번째 요소에 대한 인덱스를 반환 즉, 찾고자 하는 값의 첫 번째 인덱스를 가져온다. 구문 arr.findIndex(callback(요소[, 인덱스[, 배열]])[, callback을 실행할 때 this로 사용할 값]) 예1) const arr = ['item1', 'item2', 'item3']; console.log(arr.findIndex((e) => e == 'item3')); 결과 : 2 console.log(arr.findIndex((e) => e == 'item1')); 결과 : 0 예2) const nums = [1, 5, 6, 9, 11, 15, 4, 3, 2]; find1 = nums.findIndex((e, index)..

개발/javascript 2021.12.07
728x90