728x90
아래와 같이 오브젝트 배열 속에서 원하는 요소만 갖는 배열을 만들 수가 있다.
const people = [
{
name: '홍길동',
age: 32,
job: '무직',
married: true,
},
{
name: '김도끼',
age: 20,
job: '사무직',
married: false,
},
{
name: '하아늘',
age: 19,
job: '학생',
married: false,
}
];
// 미혼만 추출
const happyPeople = people.filter((friend) => {
return people.married === false;
})
console.log('현재 미혼입니다. ', happyPeople);
ps. Object를 copy하려면 아래와 같이 하면 된다.
// assign(target, source);
let obj: object = Object.assign({}, req.query);
Reference
JavaScript - 배열 filter() 사용법 및 예제
구문 자바스크립트의 filter함수는 배열의 각 요소를 순회하며 callback함수를 실행하며 조건에 맞는 요소만을 갖는 배열을 반환합니다. arr.filter(callback(element[, index[, array]])[, thisArg]) 파라미터 callbac
tocomo.tistory.com
728x90
728x90
'기술공부 > 언어' 카테고리의 다른 글
[Node.js] jsonpath 패키지 사용 시 pkg build Error (Error: Cannot find module 'esprima' Require stack:- aesprim) 발생 해결 방법 (0) | 2024.06.25 |
---|---|
[Node.js] Express app에서 정적 라우트 경로값을 동적으로 변경하기 (0) | 2023.06.16 |
[JavaScript] Array 중복 제거 (0) | 2023.02.21 |
[C#] 라이브러리 만들어보기 - enum(열거형) (0) | 2022.04.26 |
[C#] List slicing - Skip과 Take (0) | 2022.04.02 |