2017-10-03 7 views
0

객체의 키 값을 통해 필터링하려는 객체의 객체가 있습니다. 내가 creditid하여 객체를 필터링 할 수 있도록하려는필터를 사용한 JS 객체 소멸

const Financials = { 
    xxxxx: { 
    creditid: "yyyy", 
    aggRevs: 2000, 
    aggexpenses: 1000, 
    dateOf: 12/31/2015 
    }, 
    zzzz: { 
    creditid: "yyyy", 
    aggRevs: 1000, 
    aggexpenses: 1000, 
    dateOf: 12/31/2016 
    }, 
    aaaa: { 
    creditid: "bbbb", 
    aggRevs: 1000, 
    aggexpenses: 1000, 
    dateOf: 12/31/2016 
    } 
}; 

: 같은 예를 들어, 내 개체가 보인다. 예를 들어, "yyyy"의 creditid를 가진 모든 객체를 포함하는 객체를 반환하고자합니다.

var { creditid: "yyyy" } = Financials; 

그리고 결과는 같을 것이다

{ 
    xxxxx: { 
    creditid: "yyyy", 
    aggRevs: 2000, 
    aggexpenses: 1000, 
    dateOf: 12/31/2015 
    }, 
    zzzz: { 
    creditid: "yyyy", 
    aggRevs: 1000, 
    aggexpenses: 1000, 
    dateOf: 12/31/2016 
    } 
} 

이 가능하여 destructuring인가? 이를 위해

+0

는'이 가능하여 destructuring인가'-? 나는 생각하지 않는다의 영형. 개체를 완전히 복사하거나 포함 된 개체를 복사하지 않겠습니까? 원래 '금융'편집은 'const'와 같지 않습니다. – ASDFGerte

+0

원본을 편집하고 싶습니다. 나는 creditid에 일치하는 키/값 쌍이있는 객체를 업데이트/편집하려고합니다. 나는 파괴가 그것을 시도하는 청결한 방법일지도 모른다고 생각했다. – Vince

+1

'creditid에 일치하는 키/값 쌍이있는 객체를 업데이트/편집하려고합니다. - 이것이 [XY 문제] (https://meta.stackexchange.com/questions/66377/what-is-the)입니다. -xy-problem)? – ASDFGerte

답변

0

지금까지 destructuring이가는대로, 나는 destructuring 더 .map()를 같이 작동 간단하기 때문에이, 할 수 있다는 것을 알고하지 않습니다 .filter()보다. 그러나 실제로 그렇게처럼 .reduce() 기능을 아주 쉽게이 작업을 수행 할 수 있습니다

const Financials = { 
 
    xxxxx: { 
 
    creditid: "yyyy", 
 
    aggRevs: 2000, 
 
    aggexpenses: 1000, 
 
    dateOf: 12/31/2015 
 
    }, 
 
    zzzz: { 
 
    creditid: "yyyy", 
 
    aggRevs: 1000, 
 
    aggexpenses: 1000, 
 
    dateOf: 12/31/2016 
 
    }, 
 
    aaaa: { 
 
    creditid: "bbbb", 
 
    aggRevs: 1000, 
 
    aggexpenses: 1000, 
 
    dateOf: 12/31/2016 
 
    } 
 
}; 
 

 
var filtered = Object.keys(Financials).reduce((res, key) => { 
 
    if (Financials[key].creditid === "yyyy") { 
 
    res[key] = Financials[key] 
 
    } 
 
    return res; 
 
}, {}); 
 

 
console.log(filtered);

0

다음과 같이 당신은 금융의 각 속성을 반복해야합니다 :

const Financials = { 
    xxxxx: { 
    creditid: "yyyy", 
    aggRevs: 2000, 
    aggexpenses: 1000, 
    dateOf: '12/31/2015' 
    }, 
    zzzz: { 
    creditid: "yyyy", 
    aggRevs: 1000, 
    aggexpenses: 1000, 
    dateOf: '12/31/2016' 
    }, 
    aaaa: { 
    creditid: "bbbb", 
    aggRevs: 1000, 
    aggexpenses: 1000, 
    dateOf: '12/31/2016' 
    } 
}; 

var resultFinancials = {}; 

for (var financial in Financials) { 
    if (Financials.hasOwnProperty(financial)) { 
     if(Financials[financial] && Financials[financial].creditid =='yyyy'){ 
      resultFinancials[financial] = Financials[financial]; 
     } 
    } 
} 

console.log(resultFinancials) 
0

당신은 객체 항목을 필터링 한 다음 새 개체에 다시 매핑 할 수 있습니다

const Financials = { 
 
    xxxxx: { 
 
    creditid: "yyyy", 
 
    aggRevs: 2000, 
 
    aggexpenses: 1000, 
 
    dateOf: 12/31/2015 
 
    }, 
 
    zzzz: { 
 
    creditid: "yyyy", 
 
    aggRevs: 1000, 
 
    aggexpenses: 1000, 
 
    dateOf: 12/31/2016 
 
    }, 
 
    aaaa: { 
 
    creditid: "bbbb", 
 
    aggRevs: 1000, 
 
    aggexpenses: 1000, 
 
    dateOf: 12/31/2016 
 
    } 
 
}; 
 

 
let arr = Object.entries(Financials).filter(set => set[1].creditid === "yyyy"); 
 
let result = Object.assign(...arr.map(d => ({[d[0]]: d[1]}))) 
 

 
console.log(result)
.as-console-wrapper {top:0; max-height: 100%!important}