1

This is my data array :

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

How do I get all CodeDescription's of the objects where the CodeType has the value "HouseOwn"?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Dreams
  • 8,288
  • 10
  • 45
  • 71
  • 1
    Possible duplicate of [Sort array of objects by string property value in JavaScript](http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) – jperezov Feb 16 '16 at 17:19
  • Do you literally mean sort, or do you mean you only want those codetypes? – Sterling Archer Feb 16 '16 at 17:19
  • Yes, I want sort all data and get all CodeDescription which its codeType is HouseOwn. – Dreams Feb 16 '16 at 17:21

2 Answers2

4

You can filter and map your data (used ES6 syntax):

let descriptions = data.filter(item => item.CodeType === "HouseOwn")
                       .map(item => item.CodeDescription);

console.log(descriptions); // Array [ "Spouse", "Parent", "Own" ]

ES5 analogy:

var descriptions = data.filter(function(item) {
    return item.CodeType === "HouseOwn";
}).map(function(item) {
    return item.CodeDescription;
});

console.log(descriptions); // Array [ "Spouse", "Parent", "Own" ]
madox2
  • 49,493
  • 17
  • 99
  • 99
2

Something like this:

var codeDescriptionArray = theArrayofObjects
  .filter(function(obj) {
    return obj.codeType === "HouseOwn";
  })
  .map(function(obj) {
    return obj.codeDescription;
  });

This is an awesome tutorial that will help you practice sorting techniques using functional programming: http://reactivex.io/learnrx/

Gabriel Kunkel
  • 2,643
  • 5
  • 25
  • 47