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
3 Answers
Reset to default 2You 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
评论列表(0条)