gpt4 book ai didi

javascript - 过滤 JSON 文件中的数据

转载 作者:行者123 更新时间:2023-11-30 09:35:37 24 4
gpt4 key购买 nike

我目前正在开发一个使用 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
},

例如,我想要 for Orga_Id: "86094"JSON 文件中的所有数据。我唯一能做的就是把所有的数据都拿来,例如:

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

最佳答案

你可以使用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);

关于javascript - 过滤 JSON 文件中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43754143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com