过滤JSON文件中的数据

时间:2017-05-03 07:46:38

标签: javascript json d3.js

我目前正在使用JSON文件开发一个小应用程序。我的数据有问题。我必须过滤我的数据。例如,我想要某个用户ID的所有数据,但我不知道如何做到这一点。 以下是我的JSON文件示例:

[{
    "ConsoPhot_Id": "7924",
    "idLotImport": 166,
    "Date_Id": 20160601,
    "Orga_Id": "86094",
    "NbTache": 35,
    "NbCopie": 143,
    "NbCopieBW": 56,
    "NbCopieCouleur": 87,
    "MtTotal": 3.53
},
{
    "ConsoPhot_Id": "7925",
    "idLotImport": 166,
    "Date_Id": 20160601,
    "Orga_Id": "86537",
    "NbTache": 291,
    "NbCopie": 969,
    "NbCopieBW": 622,
    "NbCopieCouleur": 347,
    "MtTotal": 15.61
},
{
    "ConsoPhot_Id": "7926",
    "idLotImport": 166,
    "Date_Id": 20160601,
    "Orga_Id": "86386",
    "NbTache": 7,
    "NbCopie": 32,
    "NbCopieBW": 31,
    "NbCopieCouleur": 1,
    "MtTotal": 0.16
},
{
    "ConsoPhot_Id": "7927",
    "idLotImport": 166,
    "Date_Id": 20160601,
    "Orga_Id": "86084",
    "NbTache": 2,
    "NbCopie": 3,
    "NbCopieBW": 3,
    "NbCopieCouleur": 0,
    "MtTotal": 0.01
},
{
    "ConsoPhot_Id": "7928",
    "idLotImport": 166,
    "Date_Id": 20160701,
    "Orga_Id": "86094",
    "NbTache": 33,
    "NbCopie": 68,
    "NbCopieBW": 31,
    "NbCopieCouleur": 37,
    "MtTotal": 1.53
},

例如,我想要Orga_Id:" 86094" JSON文件中的所有数据。我唯一能做的就是用这个来获取所有数据:

d3.json("Data.json", function(error, data) {
    var NbCopie = data.map(function(d) {
        return d.NbCopie;
    });

3 个答案:

答案 0 :(得分:2)

可以使用本机数组方法Array.filter

进行数组过滤
     var NbCopie = data.filter(function(value) {
            return value.Orga_Id == "86094";
     });

答案 1 :(得分:2)

您可以使用Array#filter()

以下代码位于ES6



const data =[{"ConsoPhot_Id":"7924","idLotImport":166,"Date_Id":20160601,"Orga_Id":"86094","NbTache":35,"NbCopie":143,"NbCopieBW":56,"NbCopieCouleur":87,"MtTotal":3.53},{"ConsoPhot_Id":"7925","idLotImport":166,"Date_Id":20160601,"Orga_Id":"86537","NbTache":291,"NbCopie":969,"NbCopieBW":622,"NbCopieCouleur":347,"MtTotal":15.61},{"ConsoPhot_Id":"7926","idLotImport":166,"Date_Id":20160601,"Orga_Id":"86386","NbTache":7,"NbCopie":32,"NbCopieBW":31,"NbCopieCouleur":1,"MtTotal":0.16},{"ConsoPhot_Id":"7927","idLotImport":166,"Date_Id":20160601,"Orga_Id":"86084","NbTache":2,"NbCopie":3,"NbCopieBW":3,"NbCopieCouleur":0,"MtTotal":0.01},{"ConsoPhot_Id":"7928","idLotImport":166,"Date_Id":20160701,"Orga_Id":"86094","NbTache":33,"NbCopie":68,"NbCopieBW":31,"NbCopieCouleur":37,"MtTotal":1.53}];

const key = "Orga_Id";
const value= "86094";
const result = data.filter(d=>d[key]==value);

console.log(result);




答案 2 :(得分:0)

你可以像这样使用jquery

   **$.each(data, function (i, data) {
                 if (data.Orga_Id == "86094")
                 //then the data;
             });**