按键名过滤JSON数据

时间:2016-01-18 08:23:16

标签: javascript angularjs json

我是angularJS的新手

我有这样的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",
}
]

如何过滤此数据并存储所有" REPORT_STATUS"数据在一个单独的数组中。

我的数组应该包含: [" REPORT_STATUS":1," REPORT_STATUS":4," REPORT_STATUS":5," REPORT_STATUS":1]

4 个答案:

答案 0 :(得分:2)

让我们玩builded functionality

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

将产生

[{"REPORT_STATUS":1},{"REPORT_STATUS":4},{"REPORT_STATUS":5},{"REPORT_STATUS":1}]

答案 1 :(得分:0)

var reportStatuses = [];
angular.forEach(myJson, function(jsonObj) {
    reportStatuses.push(jsonObj.REPORT_STATUS);
});

通过这种方式,您将拥有一个包含所有报告状态的数组,如下所示:

[1,2,4,6,7]

不可能有一个带键值对的数组,为此使用一个对象。

答案 2 :(得分:0)

试试这个

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

REPORT_STATUS将存储在状态变量输出status = [1,4,5,1]

了解更多知识https://docs.angularjs.org/api/ng/function/angular.forEach

答案 3 :(得分:0)

单行版

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

这将为您提供[1,2,3,4,5]

或者:

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

会给你[{"REPORT_STATUS": 1},{"REPORT_STATUS": 2} ... ]