简体   繁体   English

按键名过滤JSON数据

[英]filtering JSON data by key name

I am new to angularJS 我是angularJS的新手

I have JSON data like this : 我有这样的JSON数据:

[
{
    "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. 如何过滤此数据并将所有“ REPORT_STATUS”数据存储在单独的数组中。

My array should contain : ["REPORT_STATUS": 1,"REPORT_STATUS": 4,"REPORT_STATUS": 5,"REPORT_STATUS": 1] 我的数组应包含:[“ REPORT_STATUS”:1,“ REPORT_STATUS”:4,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: 这样,您将拥有一个包含所有reportstatus的数组,如下所示:

[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] REPORT_STATUS将在状态变量中存储输出status = [1,4,5,1]

For more know https://docs.angularjs.org/api/ng/function/angular.forEach 有关更多信息, 参见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] 这会给你[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} ... ] 会给您[{"REPORT_STATUS": 1},{"REPORT_STATUS": 2} ... ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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