본문 바로가기

Object3

reduce로 객체 같은 값끼리 분류 const myArray = [ { group: "one", color: "red" }, { group: "two", color: "blue" }, { group: "one", color: "green" }, { group: "one", color: "black" } ]; // 배열.reduce((누적값, 현잿값, 인덱스, 요소) => { return 결과 }, 초깃값); const groupValues = myArray.reduce((acc, current) => { acc[current.group] = acc[current.group] || []; acc[current.group].push(current.color); return acc; }, {}); // 위에서 만든 객체를 key로 돌려서 새로운.. 2021. 10. 26.
객체의 모든 속성이 null이나 빈값인지 확인 var obj = { x: "123", y: "aa", } var obj2 = { x: null, y: "aa", } const isEmpty = (object) => !Object.values(object).every(x => (x !== null && x !== '')); console.log(isEmpty(obj)); // false console.log(isEmpty(obj2)); // true object.value()로 객체의 모든 속성값을 배열로 반환합니다. 그리고 Array.every()로 배열을 확인합니다. 2021. 3. 29.
배열에서 원하는 객체 찾기 find() 메서드를 이용해 배열안에 원하는 객체를 찾습니다. find()는 일치하는 첫번째 요소만 반환해줍니다. const arr = [{ a: 1, b: 2 }, { a: 3, b: 4 }, { a: 5, b: 6 }, { a: 7, b: 8 } ]; const result = arr.find(obj => { return obj.b === 6; // { a: 5, b: 6 } }); 원하는 값을 가진 모든 객체를 반환하려면 filter()를 이용하면 됩니다. filter()는 새 배열을 만들어 리턴해줍니다. const arr = [ { name: 'string 1', value: '1,2' }, { name: 'string 2', value: '2' }, { name: 'string 2', value.. 2020. 9. 10.