1

I am trying to filter id = 963 from the following json object. But getting empty array as output. Here is the code:

 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);

This this json contains arrays of objects inside array. I am trying to get one specific object. But getting empty array in console.

[]
1
  • 1
    id is string so try item.id == 963 or item.id === '963' Commented Dec 6, 2019 at 7:12

2 Answers 2

2

At first you should concatenate all the winners then find your item:

var winners = this.json.prizes.reduce((aggr, holders) => (aggr.push(...holders.laureates), aggr), []);
var winner = winners.filter(item => item.id == '963');
Sign up to request clarification or add additional context in comments.

1 Comment

I would use find instead of filter... data.prizes.reduce((acc, winners) => (acc.push(...winners.laureates), acc), []).find(i => i.id === '963')
0
    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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.