首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从包含对象数组的json中获取特定对象

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

Stack Overflow用户
提问于 2019-12-06 15:09:01
回答 2查看 110关注 0票数 1

我试图从下面的json对象中过滤id = 963。而是将空数组作为输出。代码如下:

代码语言:javascript
复制
 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包含数组中的对象数组。我正在尝试获取一个特定的对象。但在控制台中获取空数组。

代码语言:javascript
复制
[]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-06 15:21:20

首先,你应该连接所有的获胜者,然后找到你的物品:

代码语言:javascript
复制
var winners = this.json.prizes.reduce((aggr, holders) => (aggr.push(...holders.laureates), aggr), []);
var winner = winners.filter(item => item.id == '963');
票数 2
EN

Stack Overflow用户

发布于 2019-12-06 16:30:13

代码语言:javascript
复制
    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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59208464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档