web-dev-qa-db-fra.com

Filtrer JSON par valeur de clé

J'ai le code JSON suivant et je souhaite conserver le tableau dans le même format mais inclure uniquement des objets de type "ar". Comment puis-je filtrer ce JSON avec JavaScript pour n'inclure que les types dont j'ai besoin?

[{"time":"2016-07-26 09:02:27","type":"aa"},
{"time":"2016-04-21 20:35:07","type":"ae"},
{"time":"2016-08-20 03:31:57","type":"ar"},
{"time":"2017-01-19 22:58:06","type":"ae"},
{"time":"2016-08-28 10:19:27","type":"ae"},
{"time":"2016-12-06 10:36:22","type":"ar"},
{"time":"2016-07-09 12:14:03","type":"ar"},
{"time":"2016-10-25 05:05:37","type":"ae"},
{"time":"2016-06-05 07:57:18","type":"ae"},
{"time":"2016-10-08 22:03:03","type":"aa"},
{"time":"2016-08-13 21:27:37","type":"ae"},
{"time":"2016-04-09 07:36:16","type":"ar"},
{"time":"2016-12-30 17:20:08","type":"aa"},
{"time":"2016-03-11 17:31:46","type":"aa"},
{"time":"2016-05-04 14:08:25","type":"ar"},
{"time":"2016-11-29 05:21:02","type":"ar"},
{"time":"2016-03-08 05:46:01","type":"ar"},
]
9
JordanBarber

Vous devriez utiliser la méthode filter.

La méthode filter() crée une nouvelle array avec tous les éléments qui réussissent le test implémenté par la fonction fournie.

Fourni function est une callback qui est appliquée à chaque élément du tableau.

var arr=[{"time":"2016-07-26 09:02:27","type":"aa"},
{"time":"2016-04-21 20:35:07","type":"ae"},
{"time":"2016-08-20 03:31:57","type":"ar"},
{"time":"2017-01-19 22:58:06","type":"ae"},
{"time":"2016-08-28 10:19:27","type":"ae"},
{"time":"2016-12-06 10:36:22","type":"ar"},
{"time":"2016-07-09 12:14:03","type":"ar"},
{"time":"2016-10-25 05:05:37","type":"ae"},
{"time":"2016-06-05 07:57:18","type":"ae"},
{"time":"2016-10-08 22:03:03","type":"aa"},
{"time":"2016-08-13 21:27:37","type":"ae"},
{"time":"2016-04-09 07:36:16","type":"ar"},
{"time":"2016-12-30 17:20:08","type":"aa"},
{"time":"2016-03-11 17:31:46","type":"aa"},
{"time":"2016-05-04 14:08:25","type":"ar"},
{"time":"2016-11-29 05:21:02","type":"ar"},
{"time":"2016-03-08 05:46:01","type":"ar"},
];
var filtered=arr.filter(function(item){
    return item.type=="ar";         
});
console.log(filtered);

En outre, vous pouvez utiliser une méthode shorter:

var filtered = arr.filter(a=>a.type=="ar");
13

Vous pouvez utiliser la fonction filter. Vérifiez support du navigateur first:

var result = a.filter(function(v){
    return v.type==='ar';
})

J'espère que cela t'aides.

var list = [
  {"time":"2016-07-26 09:02:27","type":"aa"},
  {"time":"2016-04-21 20:35:07","type":"ae"},
  {"time":"2016-08-20 03:31:57","type":"ar"},
  {"time":"2017-01-19 22:58:06","type":"ae"},
  {"time":"2016-08-28 10:19:27","type":"ae"},
  {"time":"2016-12-06 10:36:22","type":"ar"},
  {"time":"2016-07-09 12:14:03","type":"ar"},
  {"time":"2016-10-25 05:05:37","type":"ae"},
  {"time":"2016-06-05 07:57:18","type":"ae"},
  {"time":"2016-10-08 22:03:03","type":"aa"},
  {"time":"2016-08-13 21:27:37","type":"ae"},
  {"time":"2016-04-09 07:36:16","type":"ar"},
  {"time":"2016-12-30 17:20:08","type":"aa"},
  {"time":"2016-03-11 17:31:46","type":"aa"},
  {"time":"2016-05-04 14:08:25","type":"ar"},
  {"time":"2016-11-29 05:21:02","type":"ar"},
  {"time":"2016-03-08 05:4601","type":"ar"},
];

var result = list.filter(function(obj, index){
  return obj.type==='ar';
})

console.log(result);

2
Zakaria Acharki

En utilisant la méthode filter, vous pouvez filtrer le tableau pour ne renvoyer que les éléments qui correspondent à une condition particulière data.filter((x)=>x.type === "ar");

La méthode filter crée un nouveau tableau avec tous les éléments qui passeront la condition x.type === "ar"

var data =[{"time":"2016-07-26 09:02:27","type":"aa"},
{"time":"2016-04-21 20:35:07","type":"ae"},
{"time":"2016-08-20 03:31:57","type":"ar"},
{"time":"2017-01-19 22:58:06","type":"ae"},
{"time":"2016-08-28 10:19:27","type":"ae"},
{"time":"2016-12-06 10:36:22","type":"ar"},
{"time":"2016-07-09 12:14:03","type":"ar"},
{"time":"2016-10-25 05:05:37","type":"ae"},
{"time":"2016-06-05 07:57:18","type":"ae"},
{"time":"2016-10-08 22:03:03","type":"aa"},
{"time":"2016-08-13 21:27:37","type":"ae"},
{"time":"2016-04-09 07:36:16","type":"ar"},
{"time":"2016-12-30 17:20:08","type":"aa"},
{"time":"2016-03-11 17:31:46","type":"aa"},
{"time":"2016-05-04 14:08:25","type":"ar"},
{"time":"2016-11-29 05:21:02","type":"ar"},
{"time":"2016-03-08 05:46:01","type":"ar"},
];
           
var result = data.filter((x)=>x.type === "ar");
console.log(result);

1
Abhinav Galodha

Pas aussi propre que la réponse ci-dessus, mais fera toujours le travail

var list = [{"time":"2016-07-26 09:02:27","type":"aa"},
{"time":"2016-04-21 20:35:07","type":"ae"},
{"time":"2016-08-20 03:31:57","type":"ar"},
{"time":"2017-01-19 22:58:06","type":"ae"},
{"time":"2016-08-28 10:19:27","type":"ae"},
{"time":"2016-12-06 10:36:22","type":"ar"},
{"time":"2016-07-09 12:14:03","type":"ar"},
{"time":"2016-10-25 05:05:37","type":"ae"},
{"time":"2016-06-05 07:57:18","type":"ae"},
{"time":"2016-10-08 22:03:03","type":"aa"},
{"time":"2016-08-13 21:27:37","type":"ae"},
{"time":"2016-04-09 07:36:16","type":"ar"},
{"time":"2016-12-30 17:20:08","type":"aa"},
{"time":"2016-03-11 17:31:46","type":"aa"},
{"time":"2016-05-04 14:08:25","type":"ar"},
{"time":"2016-11-29 05:21:02","type":"ar"},
{"time":"2016-03-08 05:46:01","type":"ar"},
]

var newList = [];

for (var i = 0; i < list.length; i++) {
    if (list[i].type === 'ar') {
        newList.Push(list[i]);
    }
}

console.log(newList);
0
user2085143