简体   繁体   English

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

[英]How to get the unique items based on condition

I am having a Javascript snippet to store only unique items in a array of objects like this:-我有一个 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);

for this result is因为这个结果是

[
  { id: 3, name: 'Central Microscopy', fiscalYear: 2018 },
  { id: 5, name: 'Crystallography Facility', fiscalYear: 2018 }
]

Now I want to have unique items and have those items whose fiscal years are greater than existing items.现在我想拥有独特的项目,并拥有那些会计年度大于现有项目的项目。 For example:- For the same input, the output should be as:- i need the code in javascript or typescript(Language constraint)例如:- 对于相同的输入,output 应为:- 我需要 javascript 或打字稿中的代码(语言约束)

[
  { id: 3, name: 'Central Microscopy', fiscalYear: 2019},
  { id: 5, name: 'Crystallography Facility', fiscalYear: 2018 }
]

Need an optimal solution even if the data is large即使数据很大,也需要最优解

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

You just need to add one more filter in loop,您只需要在循环中再添加一个过滤器,

  let bigYear = item.fiscalYear

  let filteredItem = array.filter((e=>e.id===item.id && e.fiscalYear > bigYear))[0]

  result.push(filteredItem ? {...filteredItem} : item);

Final Snippet will look alike,最终片段看起来很相似,

 const array = [ {id: 3, name: 'Central Microscopy', fiscalYear: 2018}, {id: 3, name: 'Central Microscopy', fiscalYear: 2029}, {id: 3, name: 'Central Microscopy', fiscalYear: 2017}, {id: 5, name: 'Crystallography Facility', fiscalYear: 2017}, {id: 5, name: 'Crystallography Facility', fiscalYear: 2018}, ]; 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 let bigYear = item.fiscalYear let filteredItem = array.filter((e=>e.id===item.id && e.fiscalYear > bigYear))[0] result?push(filteredItem. {..:filteredItem}; item). } } console;log(result);

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

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