【问题标题】:How to get the unique items based on condition如何根据条件获得独特的物品
【发布时间】:2020-07-12 17:13:50
【问题描述】:

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

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

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

【问题讨论】:

  • 您的问题措辞就像您在分配任务一样。你做了什么?这不是麦当劳的得来速餐厅。
  • 我觉得我要求一些改进。如果可能,请尝试帮助我
  • 您向我们提供了一系列要求,即代码应该采用某种语言,并且应该是最佳的。您还没有展示您尝试过的内容,以及您尝试的解决方案没有解决的问题。
  • 我已经给出我的解决方案输出是 ` [ { id: 3, name: 'Central Microscopy', accountingYear: 2018 }, { id: 5, name: 'Crystallography Facility', accountingYear: 2018 } ] ` 现在我需要输出为 ``` [ { id: 3, name: 'Central Microscopy', accountingYear: 2019}, { id: 5, name: 'Crystallography Facility', accountingYear: 2018 } ] `` ` 我在其中涉及一个应该接受的更大财政年的条件
  • JavaScript 或 typescript 都被接受(其中任何一个)

标签: javascript arrays ecmascript-6 filter unique-constraint


【解决方案1】:

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

【讨论】:

  • 我认为你错过了年度要求,但不能怪你。带有边缘描述并且没有尝试解决它,很容易错过。
  • 我假设,我已经涵盖了您试图指出的情况。不过,如果我错过了,请指出。
  • 好吧,他们接受了你的回答,但我认为他们不明白。没有迹象表明他们想要任意年份作为过滤器。当人们要求代码而没有尝试自己解决时,这就是问题。
  • 我认为你是一个难以理解解决方案的人。它很清楚并且涵盖了所有情况。尝试运行代码 sn-p 并告诉我你认为它可能失败的情况。我会很高兴知道。
  • 那么如果所有具有特定 ID 的对象的年份都小于 2018 年?
【解决方案2】:

你只需要在循环中再添加一个过滤器,

  let bigYear = item.fiscalYear

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

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

最终片段看起来很相似,

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

【讨论】:

  • 您已经在使用Map,为什么还要使用过滤器呢?您可以检查当前的item 是否大于地图中的@(如果有)。
  • 这里我们不会将每个项目都添加到Map 中,我们使用Map 作为中介来验证具有唯一ID 的项目。对于最近一年,我们需要遍历原始数组以获得最近一年的唯一项目。
  • 哦,对了,你还在设置true。我的意思是,既然您使用的是地图,那么将对象设置为值,然后您就可以进行直接比较。无需每次都过滤。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多