javascript - filter data in a JSON file - Stack Overflow

I'm currently developing a small application using a JSON file. I have a problem with my data. I m

I'm currently developing a small application using a JSON file. I have a problem with my data. I must filter my data. For example, I want all the data for a certain User Id but I don't know how doing that. Here is an example of my JSON file:

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

For example, I want for Orga_Id: "86094" all the data in the JSON file. The only thing that i can do is taking all the data with this for example:

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

I'm currently developing a small application using a JSON file. I have a problem with my data. I must filter my data. For example, I want all the data for a certain User Id but I don't know how doing that. Here is an example of my JSON file:

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

For example, I want for Orga_Id: "86094" all the data in the JSON file. The only thing that i can do is taking all the data with this for example:

d3.json("Data.json", function(error, data) {
    var NbCopie = data.map(function(d) {
        return d.NbCopie;
    });
Share Improve this question edited May 6, 2017 at 14:50 Mark Amery 156k90 gold badges430 silver badges472 bronze badges asked May 3, 2017 at 7:46 A.NicolasA.Nicolas 711 gold badge1 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You can use Array#filter()

The following code is in 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);

can do a array filter with native array method Array.filter

     var NbCopie = data.filter(function(value) {
            return value.Orga_Id == "86094";
     });

you can use jquery for the same like this

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744146760a4560460.html

相关推荐

  • javascript - filter data in a JSON file - Stack Overflow

    I'm currently developing a small application using a JSON file. I have a problem with my data. I m

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信