002_indexOf() 와 findIndex()
바쁜 분들을 위한 결론 indexOf()는 배열의 특정 값을 입력하면 그 값이 몇번째인지 알려주는 함수이다. 단, 배열 속 객체에 적용을 하면 -1(일치하는 값 없음) 이 출력된다. 이럴 때 findIndex()를 사용하면 배열 속 객체를 검색해 그 객체가 몇번째인지 알려준다. 예제 // indexOf() const alphabet = ['a', 'b', 'c', 'd', 'e', 'f']; const index = alphabet.indexOf('c'); console.log(index); // 2 // findIndex() const todos = [ { id = 1, todo = '빨래하기', finished = true }, { id = 2, todo = '자바스크립트 공부하기', finished..
2021. 7. 18.
001_forEach와 map의 차이
바쁜 분들을 위한 결론 forEach는 배열 요소를 하나씩 불러내 함수를 적용한 후 각각 내보낸다. map은 배열 요소를 하나씩 불러내 함수를 적용한 후 새로운 배열에 저장해 내보낸다. 즉 forEach는 배열 해체, map은 배열 재결합인 셈이다. 예제 // forEach const numbers = [1, 2, 3, 4, 5]; numbers.forEach(num => { console.log(num); }); // 1 // 2 // 3 // 4 // 5 // map const arr = [1, 2, 3, 4, 5]; const squared = arr.map(n => n * n); console.log(squared); // [1, 4, 9, 16, 25] 위 차이처럼 forEach는 배열의 요소들..
2021. 7. 18.