繁体   English   中英

如何从包含对象数组的 json 中获取特定对象

[英]How to fetch specific object from json containing arrays of objects

我正在尝试从以下 json 对象中过滤 id = 963。 但是将空数组作为输出。 这是代码:

 var json ={"prizes"[
{"year":"2018",
 "category":"physics",
 "overallMotivation":"\u201cfor groundbreaking inventions in the field of laser physics\u201d",
 "laureates"[
 {"id":"960",
  "firstname":"Arthur",
  "surname":"Ashkin",
  "motivation":"\"for the optical tweezers and their application to biological systems\"","share":"2"},
{"id":"961",
 "firstname":"G\u00e9rard",
 "surname":"Mourou",
 "motivation":"\"for their method of generating high-intensity, ultra-short optical pulses\"",
 "share":"4"},
 {"id":"962",
  "firstname":"Donna",
  "surname":"Strickland",
  "motivation":"\"for their method of generating high-intensity, ultra-short optical pulses\"",
  "share":"4"}]},
  {"year":"2018",
   "category":"chemistry",
  "laureates":[
  {"id":"963",
   "firstname":"Frances H.",
   "surname":"Arnold",
   "motivation":"\"for the directed evolution of enzymes\"",
   "share":"2"},
  {"id":"964",
   "firstname":"George P.",
   "surname":"Smith",
   "motivation":"\"for the phage display of peptides and antibodies\"",
   "share":"4"},
  {"id":"965",
   "firstname":"Sir Gregory P.","surname":"Winter",
   "motivation":"\"for the phage display of peptides and antibodies\"",
   "share":"4"}]},
  {"year":"2018",
   "category":"medicine",
   "laureates":[
  {"id":"958",
   "firstname":"James P.",
   "surname":"Allison",
   "motivation":"\"for their discovery of cancer therapy by inhibition of negative immune regulation\"",
   "share":"2"},
  {"id":"959",
   "firstname":"Tasuku",
   "surname":"Honjo",
   "motivation":"\"for their discovery of cancer therapy by inhibition of negative immune regulation\"",
  "share":"2"}]}]};

var winners = json.prizes.map(holders=> holders.laureates)
var winner = winners.filter(item => item.id === 963)
console.log(winner);

这个 json 包含数组内的对象数组。 我正在尝试获取一个特定对象。 但是在控制台中获取空数组。

[]

首先,您应该连接所有获奖者,然后找到您的项目:

var winners = this.json.prizes.reduce((aggr, holders) => (aggr.push(...holders.laureates), aggr), []);
var winner = winners.filter(item => item.id == '963');
    var json ={"prizes":[
{"year":"2018",
 "category":"physics",
 "overallMotivation":"\u201cfor groundbreaking inventions in the field of laser physics\u201d",
 "laureates":[
 {"id":"960",
  "firstname":"Arthur",
  "surname":"Ashkin",
  "motivation":"\"for the optical tweezers and their application to biological systems\"","share":"2"},
{"id":"961",
 "firstname":"G\u00e9rard",
 "surname":"Mourou",
 "motivation":"\"for their method of generating high-intensity, ultra-short optical pulses\"",
 "share":"4"},
 {"id":"962",
  "firstname":"Donna",
  "surname":"Strickland",
  "motivation":"\"for their method of generating high-intensity, ultra-short optical pulses\"",
  "share":"4"}]},
  {"year":"2018",
   "category":"chemistry",
  "laureates":[
  {"id":"963",
   "firstname":"Frances H.",
   "surname":"Arnold",
   "motivation":"\"for the directed evolution of enzymes\"",
   "share":"2"},
  {"id":"964",
   "firstname":"George P.",
   "surname":"Smith",
   "motivation":"\"for the phage display of peptides and antibodies\"",
   "share":"4"},
  {"id":"965",
   "firstname":"Sir Gregory P.","surname":"Winter",
   "motivation":"\"for the phage display of peptides and antibodies\"",
   "share":"4"}]},
  {"year":"2018",
   "category":"medicine",
   "laureates":[
  {"id":"958",
   "firstname":"James P.",
   "surname":"Allison",
   "motivation":"\"for their discovery of cancer therapy by inhibition of negative immune regulation\"",
   "share":"2"},
  {"id":"959",
   "firstname":"Tasuku",
   "surname":"Honjo",
   "motivation":"\"for their discovery of cancer therapy by inhibition of negative immune regulation\"",
  "share":"2"}]}]};

var winners = json.prizes.map(holders=> holders.laureates)
//var st=winners[1]
for(var f=0;f<winners.length;f++)
{
for(var c=0;c<winners[f].length;c++)
{
 //console.log(winners[f][c].id=="963")
 if(winners[f][c].id=="963")
{
  var result=winners[f][c];
}
}
}
console.log(result)
//var winner = winners.find(item => item.id == "963")
//console.log(winner);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM