这是我的数据数组:

[{
    "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"

最佳答案

您可以过滤和映射数据(使用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" ]

关于javascript - 如何对来自后端服务的数据进行排序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35438968/

10-10 18:46
查看更多