如何根据条件获得独特的物品

我有一个 Javascript 片段来仅将唯一项目存储在这样的对象数组中:-


const array = [

    {id: 3, name: 'Central Microscopy', fiscalYear: 2018},

    {id: 5, name: 'Crystallography Facility', fiscalYear: 2018},

    {id: 3, name: 'Central Microscopy', fiscalYear: 2017},

    {id: 5, name: 'Crystallography Facility', fiscalYear: 2017},

    {id: 3, name: 'Central Microscopy', fiscalYear: 2019},

  ];

  const result = [];

  const map = new Map();

  for (const item of array) {

    if (!map.has(item.id) ) {

      map.set(item.id, true); // set any value to Map

      result.push({

        id: item.id,

        name: item.name,

        fiscalYear: item.fiscalYear

      });

    }

  }

  console.log(result);

因为这个结果是


[

  { id: 3, name: 'Central Microscopy', fiscalYear: 2018 },

  { id: 5, name: 'Crystallography Facility', fiscalYear: 2018 }

]

现在我想拥有独特的项目,并拥有那些会计年度大于现有项目的项目。例如:- 对于相同的输入,输出应为:- 我需要 javascript 或 typescript 中的代码(语言约束)


[

  { id: 3, name: 'Central Microscopy', fiscalYear: 2019},

  { id: 5, name: 'Crystallography Facility', fiscalYear: 2018 }

]

即使数据很大,也需要最优解


ITMISS
浏览 58回答 2
2回答

慕容708150

let array = [&nbsp; &nbsp; {id: 3, name: 'Central Microscopy', fiscalYear: 2018},&nbsp; &nbsp; {id: 5, name: 'Crystallography Facility', fiscalYear: 2018},&nbsp; &nbsp; {id: 3, name: 'Central Microscopy', fiscalYear: 2027},&nbsp; &nbsp; {id: 3, name: 'Central Microscopy', fiscalYear: 2017},&nbsp; &nbsp; {id: 5, name: 'Crystallography Facility', fiscalYear: 2017},&nbsp; &nbsp; {id: 3, name: 'Central Microscopy', fiscalYear: 2019},&nbsp; &nbsp; {id: 3, name: 'Central Microscopy', fiscalYear: 2021},&nbsp; ];&nbsp;&nbsp;&nbsp; let temp = array.filter((elem) => elem.fiscalYear >= 2018)&nbsp; let cache = {}&nbsp; temp.forEach((elem) => {&nbsp; &nbsp; &nbsp; if(cache[elem.name]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(cache[elem.name].fiscalYear < elem.fiscalYear){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache[elem.name] = elem;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cache[elem.name] = elem;&nbsp; &nbsp; &nbsp; }&nbsp; })&nbsp; let result = Object.values(cache);&nbsp; console.log(result);

德玛西亚99

您只需要在循环中再添加一个过滤器,&nbsp; let bigYear = item.fiscalYear&nbsp; let filteredItem = array.filter((e=>e.id===item.id && e.fiscalYear > bigYear))[0]&nbsp; result.push(filteredItem ? {...filteredItem} : item);最终片段看起来很相似,const array = [&nbsp; &nbsp; {id: 3, name: 'Central Microscopy', fiscalYear: 2018},&nbsp; &nbsp; {id: 3, name: 'Central Microscopy', fiscalYear: 2029},&nbsp; &nbsp; {id: 3, name: 'Central Microscopy', fiscalYear: 2017},&nbsp; &nbsp; {id: 5, name: 'Crystallography Facility', fiscalYear: 2017},&nbsp; &nbsp; {id: 5, name: 'Crystallography Facility', fiscalYear: 2018},&nbsp; ];&nbsp; const result = [];&nbsp; const map = new Map();&nbsp; for (const item of array) {&nbsp; &nbsp; if (!map.has(item.id) ) {&nbsp; &nbsp; &nbsp; map.set(item.id, true); // set any value to Map&nbsp; &nbsp; &nbsp; let bigYear = item.fiscalYear&nbsp; &nbsp; &nbsp; let filteredItem = array.filter((e=>e.id===item.id && e.fiscalYear > bigYear))[0]&nbsp; &nbsp; &nbsp; result.push(filteredItem ? {...filteredItem} : item);&nbsp; &nbsp; }&nbsp; }&nbsp; console.log(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript