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

Arrays 如何从包含对象数组的json中获取特定对象,arrays,node.js,json,Arrays,Node.js,Json,我试图从以下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",

我试图从以下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={“奖品”:[
{“年份”:“2018年”,
“类别”:“物理学”,
“总体动机”:“\u201c激光物理领域的突破性发明\u201d”,
“获奖者”:[
{“id”:“960”,
“名字”:“亚瑟”,
“姓”:“阿什金”,
“动机”:“光镊及其在生物系统中的应用”,“共享”:“2”},
{“id”:“961”,
“名字”:“G\u00e9rard”,
“姓氏”:“Mourou”,
“动机”:“他们产生高强度超短光脉冲的方法”,
“共享”:“4”},
{“id”:“962”,
“名字”:“唐娜”,
“姓氏”:“斯特里克兰”,
“动机”:“他们产生高强度超短光脉冲的方法”,
“共享”:“4”}]},
{“年份”:“2018年”,
“类别”:“化学”,
“获奖者”:[
{“id”:“963”,
“名字”:“弗朗西斯H.”,
“姓”:“阿诺德”,
“动机”:“酶的定向进化”,
“共享”:“2”},
{“id”:“964”,
“名字”:“乔治P.”,
“姓氏”:“史密斯”,
“动机”:“用于肽和抗体的噬菌体展示”,
“共享”:“4”},
{“id”:“965”,
“姓”:“格雷戈里爵士P.,“姓”:“温特”,
“动机”:“用于肽和抗体的噬菌体展示”,
“共享”:“4”}]},
{“年份”:“2018年”,
“类别”:“药物”,
“获奖者”:[
{“id”:“958”,
“名字”:“James P.”,
“姓氏”:“埃里森”,
“动机”:“他们发现通过抑制负性免疫调节进行癌症治疗”,
“共享”:“2”},
{“id”:“959”,
“名字”:“Tasuku”,
“姓”:“Honjo”,
“动机”:“他们发现通过抑制负性免疫调节进行癌症治疗”,
“共享”:“2”}]}]};
var winners=json.prices.map(holders=>holders.laureates)
//var st=优胜者[1]

对于(var f=0;fid是字符串,因此请尝试item.id==963或item.id=='963',我将使用find而不是filter…data.奖品。reduce((acc,winners)=>(acc.push(…winners.laureates),acc),[])。find(I=>I.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);