这是我的数据阵列:
[{
"CodeDescription": "Spouse",
"CodeID": "2",
"CodeType": "HouseOwn",
"ParentCodeID": "",
"ParentType": ""
}, {
"CodeDescription": "Parent",
"CodeID": "3",
"CodeType": "HouseOwn",
"ParentCodeID": "",
"ParentType": ""
}, {
"CodeDescription": "AAA",
"CodeID": "6",
"CodeType": "City",
"ParentCodeID": "",
"ParentType": ""
}, {
"CodeDescription": "Own",
"CodeID": "1",
"CodeType": "HouseOwn",
"ParentCodeID": "",
"ParentType": ""
}, {
"CodeDescription": "BBB",
"CodeID": "006",
"CodeType": "Area",
"ParentCodeID": "6",
"ParentType": "City"
}, {
"CodeDescription": "CCC",
"CodeID": "Z01",
"CodeType": "Area",
"ParentCodeID": "6",
"ParentType": "City"
}]
如何获取CodeDescription
具有值CodeType
的所有"HouseOwn"
个对象?
答案 0 :(得分:4)
您可以过滤和映射数据(使用ES6语法):
let descriptions = data.filter(item => item.CodeType === "HouseOwn")
.map(item => item.CodeDescription);
console.log(descriptions); // Array [ "Spouse", "Parent", "Own" ]
ES5类比:
var descriptions = data.filter(function(item) {
return item.CodeType === "HouseOwn";
}).map(function(item) {
return item.CodeDescription;
});
console.log(descriptions); // Array [ "Spouse", "Parent", "Own" ]
答案 1 :(得分:2)
这样的事情:
var codeDescriptionArray = theArrayofObjects
.filter(function(obj) {
return obj.codeType === "HouseOwn";
})
.map(function(obj) {
return obj.codeDescription;
});
这是一个很棒的教程,可以帮助您使用函数式编程练习排序技术:http://reactivex.io/learnrx/