기술공부/언어

[javascript] Object array 에서 특정 key-value를 만족하는 feature만 추출하기

봉두두 2023. 3. 3. 16:09
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

https://tocomo.tistory.com/25

 

JavaScript - 배열 filter() 사용법 및 예제

구문 자바스크립트의 filter함수는 배열의 각 요소를 순회하며 callback함수를 실행하며 조건에 맞는 요소만을 갖는 배열을 반환합니다. arr.filter(callback(element[, index[, array]])[, thisArg]) 파라미터 callbac

tocomo.tistory.com

 

728x90
728x90