简体   繁体   中英

filtering JSON data by key name

I am new to angularJS

I have JSON data like this :

[
{
    "REPORT_ID": "QDP56DSC4BK",
    "REPORT_NAME": "non, lobortis quis, pede.",
    "REPORT_STATUS": 1,
    "REPORT_TYPE": "Duis Sit Amet Ltd",
    "REPORT_DATE": "Sep 3, 2015",
    "REPORT_INGREDIENT_1": "Prednisone",
    "REPORT_INGREDIENT_2": "Alprazolam",
    "REPORT_INGREDIENT_3": "Prednisone",        

},
{
    "REPORT_ID": "JQY45UOQ8PY",
    "REPORT_NAME": "Cras dolor dolor, tempus",
    "REPORT_STATUS": 4,
    "REPORT_TYPE": "Sociis Incorporated",
    "REPORT_DATE": "Apr 26, 2015",
    "REPORT_INGREDIENT_1": "Clonazepam",
    "REPORT_INGREDIENT_2": "Hydrocodone/APAP",
    "REPORT_INGREDIENT_3": "Nuvaring",      
},
{
    "REPORT_ID": "EDE42OUH3FM",
    "REPORT_NAME": "posuere cubilia Curae; Donec",
    "REPORT_STATUS": 5,
    "REPORT_TYPE": "Pede Inc.",
    "REPORT_DATE": "May 22, 2015",
    "REPORT_INGREDIENT_1": "Furosemide",
    "REPORT_INGREDIENT_2": "Lipitor",
    "REPORT_INGREDIENT_3": "Losartan Potassium",

},
{
    "REPORT_ID": "BWQ55EIS6LS",
    "REPORT_NAME": "enim. Nunc ut erat.",
    "REPORT_STATUS": 1,
    "REPORT_TYPE": "Orci Sem Institute",
    "REPORT_DATE": "Dec 29, 2015",
    "REPORT_INGREDIENT_1": "Alprazolam",
    "REPORT_INGREDIENT_2": "Celebrex",
    "REPORT_INGREDIENT_3": "Promethazine HCl",
}
]

How can I filter this data and store all the "REPORT_STATUS" data in a separate array.

My array should contain : ["REPORT_STATUS": 1,"REPORT_STATUS": 4,"REPORT_STATUS": 5,"REPORT_STATUS": 1]

Lets play with builded functionality :

console.log(JSON.stringify(json, ['REPORT_STATUS']));

will produce

[{"REPORT_STATUS":1},{"REPORT_STATUS":4},{"REPORT_STATUS":5},{"REPORT_STATUS":1}]
var reportStatuses = [];
angular.forEach(myJson, function(jsonObj) {
    reportStatuses.push(jsonObj.REPORT_STATUS);
});

This way you will have an array with all your reportstatuses like so:

[1,2,4,6,7]

It is not possible to have an array with key-value pairs, use an object for that.

Try this

 values = your_json_array;
    var status = [];
    angular.forEach(values, function(value, key) {
      this.push(value.REPORT_STATUS);
    }, status);

The REPORT_STATUS will store in status variable output status = [1,4,5,1]

For more know https://docs.angularjs.org/api/ng/function/angular.forEach

single line version

var reportStatusArray = yourJsonArray.map(function(r){ return r['REPORT_STATUS']; });

This will give you [1,2,3,4,5]

alternatively:

var reportStatusArray = yourJsonArray.map(function(r){ return {'REPORT_STATUS': r['REPORT_STATUS']}; });

will give you [{"REPORT_STATUS": 1},{"REPORT_STATUS": 2} ... ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM