繁体   English   中英

从复杂的 json 对象中获取特定信息

[英]Fetch specific informations from complex json object

我想通过将名字和姓氏作为原始输入,从 json 数据中获取特定诺贝尔奖获得者的年份、类别、id 和份额。 我可以从下面的代码中轻松获取 id 和共享,但如何获取年份和类别。 这是我的代码:

  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"}
 ]}
 ]};

var winner = json.prizes.reduce((acc, winners) => 
(acc.push(...winners.laureates), acc), []).find(i => i.id === '960')
console.log(winner);  

试试这个代码。

let winners = [];
json.prizes.forEach(e => {
  let data = { year: e.year, category: e.category };

  const laureates = e.laureates.find( item => {
    return item.firstname === 'Arthur' && item.surname === 'Ashkin';
  });

  data = { ...data, ...laureates };

  winners.push(data);
});

console.log(winners);

上面的代码将给出如下输出:

[{
  category: "physics",
  firstname: "Arthur",
  id: "960",
  motivation: "\"for the optical tweezers and their application to biological systems\"",
  share: "2",
  surname: "Ashkin",
  year: "2018"
}]

请记住,当数据集很大时,上面的代码不会表现得很好。

为了完整起见,我在测试输入中添加了多个奖品。 我希望没关系。 试试这个代码。

const input = {
  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: "2019",
      category: "chemistry",
      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: "123",
          firstname: "Donna",
          surname: "Strickland",
          motivation:
            '"for their method of generating high-intensity, ultra-short optical pulses"',
          share: "6"
        }
      ]
    }
  ]
};

function find(firstName, surname) {
  return input.prizes.map(prize => {
    const filteredLaureate = prize.laureates.filter(
      laureate =>
        laureate.firstname === firstName && laureate.surname === surname
    );
    return filteredLaureate.map(i => {
      return {
        id: i.id,
        share: i.share,
        category: prize.category,
        year: prize.year
      };
    });
  }).flat();
}

const response = find("Donna", "Strickland");
console.log(response);

暂无
暂无

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

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