[
{
"Office_Id": 100,
"Address1": "xxxxx",
"Address2": "",
"City": "ANNISTON",
"District_Id": 1277,
"OfficeName": "test"
},
{
"Office_Id": 200,
"Address1": "xxxxx",
"Address2": "",
"City": "ANNISTON",
"District_Id": 1277,
"OfficeName": "test"
},
{
"Office_Id": 300,
"Address1": "xxxxx",
"Address2": "",
"City": "ANNISTON",
"District_Id": 1277,
"OfficeName": "test"
}
]
Office_Id 및 OfficeName으로 만 필터링하는 방법
lodash없이 ( 일반 자바 스크립트) 다음과 같이 :
(두 속성을 제외한 모든 것을 제거하려는 경우)
let list = [
{
"Office_Id": 100,
"Address1": "xxxxx",
"Address2": "",
"City": "ANNISTON",
"District_Id": 1277,
"OfficeName": "test"
},
{
"Office_Id": 200,
"Address1": "xxxxx",
"Address2": "",
"City": "ANNISTON",
"District_Id": 1277,
"OfficeName": "test"
},
{
"Office_Id": 300,
"Address1": "xxxxx",
"Address2": "",
"City": "ANNISTON",
"District_Id": 1277,
"OfficeName": "test"
}
];
let newList = list.map(function(currentItem){
return {"Office_Id": currentItem.Office_Id, "OfficeName": currentItem.OfficeName};
});
console.info(newList);
// Tested on Win7 64bit Chrome 57+
map
배열 개체의 기능은, 값 (이 경우 함수가 반환의 과거와, 새로운 배열을 만듭니다x => {return {"Office_Id": x.Office_Id, "OfficeName": x.OfficeName};}
). 기능에 대한 자세한 내용은 여기에서 찾을 수 있습니다. MDN 참조
주석의 상태로 람다 함수, 구조 분해 및 최적화 된 리터럴 표기법을 사용하여 코드를 최소화 할 수 있습니다. (그러나 각 기능을 사용하기 전에 뉴스 자바 스크립트 버전이 지원하는 호환성을 확인하세요.
여기에 짧은 버전 :
list.map(({Office_Id, OfficeName}) => ({Office_Id, OfficeName})});
lodash로 업데이트 :
let list = [
{
"Office_Id": 100,
"Address1": "xxxxx",
"Address2": "",
"City": "ANNISTON",
"District_Id": 1277,
"OfficeName": "test"
},
{
"Office_Id": 200,
"Address1": "xxxxx",
"Address2": "",
"City": "ANNISTON",
"District_Id": 1277,
"OfficeName": "test"
},
{
"Office_Id": 300,
"Address1": "xxxxx",
"Address2": "",
"City": "ANNISTON",
"District_Id": 1277,
"OfficeName": "test"
}
];
let newList = _.map(list, function(value){
return { "Office_Id": value.Office_Id,"OfficeName": value.OfficeName};
});
console.info(newList);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash-compat/3.10.2/lodash.js"></script>
여기 lodash지도 기능의 문서에 대한 링크
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다