第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何根據(jù)條件獲得獨(dú)特的物品

如何根據(jù)條件獲得獨(dú)特的物品

ITMISS 2022-11-11 13:10:22
我有一個(gè) Javascript 片段來僅將唯一項(xiàng)目存儲在這樣的對象數(shù)組中:-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);因?yàn)檫@個(gè)結(jié)果是[  { id: 3, name: 'Central Microscopy', fiscalYear: 2018 },  { id: 5, name: 'Crystallography Facility', fiscalYear: 2018 }]現(xiàn)在我想擁有獨(dú)特的項(xiàng)目,并擁有那些會(huì)計(jì)年度大于現(xiàn)有項(xiàng)目的項(xiàng)目。例如:- 對于相同的輸入,輸出應(yīng)為:- 我需要 javascript 或 typescript 中的代碼(語言約束)[  { id: 3, name: 'Central Microscopy', fiscalYear: 2019},  { id: 5, name: 'Crystallography Facility', fiscalYear: 2018 }]即使數(shù)據(jù)很大,也需要最優(yōu)解
查看完整描述

2 回答

?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

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


查看完整回答
反對 回復(fù) 2022-11-11
?
德瑪西亞99

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊

您只需要在循環(huán)中再添加一個(gè)過濾器,


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


查看完整回答
反對 回復(fù) 2022-11-11
  • 2 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號