목록JS (25)
taenyLog
순서를 따르지 않는다. 배열 구조 분해보다 실용적임. 사용자 정보 중 몇가지 특성에 접근하거나 선정해야 하는 경우 유용 // 예제 객체 const person = { firstName: 'John', lastName: 'Doe', age: 30, city: 'New York' }; // 객체 분해 const { firstName, lastName, age, city } = person; // 분해한 값 출력 console.log('나이:', age); console.log('도시:', city); console.log('이름:', firstName); console.log('성:', lastName); // 변수 이름 변경 const {city : country} = person; > country < ..
Copies properties from one object into another object literal 객체에 있는 특성을 복사해서 새로운 객체를 만든다. 객체 여러 개를 한 객체로 묶거나 기존의 객체를 이용해서 객체를 새롭게 만들 수 있다. 객체를 복사하거나 수정 할 수 있다 충돌시 마지막으로 온 값이 이전의 값을 덮는다. 왜 전개를 사용할까 ? 왜 새로운 객체를 만들까 ? 객체를 복사할 때 우리는 전개를 사용해서 펼친다. const dataFromForm = { email :'zzz@gmail.com', password: 'qwe123', username: 'newbie' } const newUser = {...dataFromForm, id :123, isAdmin:false } 사용자가 웹사..
In Array Literals Create a new array using an existing array. Spreads the elements from one array into a new array. 반복 가능한 객체를 어떻게 펼치는가 ? 배열을 복사해서 쓴다. const fruits = ['apple', 'banana']; const vegetables = ['cucumber','lettuce','cabbage']; const allGroceries = [...fruits, ...vagetabkes] allGroceries 호출시 'apple', 'banana' , 'cucumber','lettuce','cabbage' 가 호출된다. 배열 두개를 묶어서 새로운 배열을 만들었다.
spread : 전개구문 배열과 같이 반복 가능한 객체를 전개 구문을 사용해서 확장한다. 함수로 호출할 경우엔 0개 이상의 인수로 배열 리터럴에서는 요소로 확장할 수 있다. 객체 리터럴의 경우 객체 표현식은 0개 이상 키-값 쌍으로 확장할 수 있다. The spread (...) syntax allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected. In an object literal, the spread syntax enumerates the propertie..
const person = { firstName : 'Taeny', lastName : 'Kim', fullName : function(){ `${this.firstName} ${this.lastName}` } } 여기서 this가 person을 가르키니까 Taeny Kim의 값을 얻을 수 있다. const person = { firstName : 'Taeny', lastName : 'Kim', fullName : function() => { `${this.firstName} ${this.lastName}` } } 화살표 함수 안에 있는 this 키워드는 함수가 만든 범위를 가리킨다. 즉, 윈도우 객체를 가리킨다. const person = { firstName : 'Taeny', lastName : '..

Reduce Executes a reducer function on each element of the array, resulting in a single value. 배열을 가져다가 점차 줄여가면서 마지막에는 결국 하나의 값만 남긴다. 배열이름.reduce((accumulator,elememt) =>{ return accumulator + elememt }) 최댓값, 최솟값 찾기도 가능하다. ex) 가장 높은 / 낮은 평점을 받은 영화를 찾기 초기값 없이 reduce() const array = [15, 16, 17, 18, 19]; function reducer(accumulator, currentValue, index) { const returns = accumulator + currentValue..
map 메서드와 filter 메서드의 공통점과 차이점을 알아보자. 우선 map()는 배열의 각 요소에 대해 주어진 함수를 호출하고, 그 결과를 새로운 배열로 반환한다. 원본은 변경되지 않고 새로운 배열이 생성된다. 각 요소를 변환하고, 변환된 값을 가진 새로운 배열을 반환한다. map()의 예시 const numbers = [1, 2, 3, 4, 5]; const squaredNumbers = numbers.map(x => x * x); // squaredNumbers는 [1, 4, 9, 16, 25]가 됩니다. map()은 언제 사용하지 ? - 변환 작업 : 각 요소를 가공하거나 변환하여 새로운 값의 배열을 생성 - 데이터 가공 : 데이터를 가져와서 필요한 형식으로 가공하거나, 특정 속성을 추출하여 새..
Filter Method 필터 메서드 create a new array with all elements that pass the test implemented by the provided function Filter는 요소로 구성된 배열에서 필터링을 하거나 부분 집합을 모아 새 배열을 만드는 데 쓰임. const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter((word) => word.length > 6); console.log(result); // Expected output: Array ["exuberant", "destruction", "present"] 원본 ..