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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取數(shù)組中的公共值

如何獲取數(shù)組中的公共值

慕的地6264312 2023-06-29 21:12:05
例如我有一個數(shù)據(jù)數(shù)組如下var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]我需要在 javascript 中列出與下面相同的數(shù)據(jù)var arrDataSorted = ["40-25","50-48","30-25","40-23","40-45","40-50","40-50"]只需要刪除復(fù)制空值的公共數(shù)據(jù)。解決這個問題的最佳解決方案是什么。
查看完整描述

4 回答

?
holdtom

TA貢獻1805條經(jīng)驗 獲得超10個贊

您可以嘗試使用Array.prototype.filter()刪除值并Set獲取唯一值。最后使用 來Spread syntax (...)將集合結(jié)果轉(zhuǎn)換為數(shù)組。

嘗試以下方法:

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];


var arrDataSorted = [...new Set(arrData.filter(i => i))];

console.log(arrDataSorted);


查看完整回答
反對 回復(fù) 2023-06-29
?
海綿寶寶撒

TA貢獻1809條經(jīng)驗 獲得超8個贊

您可以從數(shù)組創(chuàng)建一個集合,該集合將自動刪除重復(fù)項:


let arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];


let set = new Set(arrData);

這仍然會保留 null,您可以通過調(diào)用將其刪除delete,并使用擴展運算符將其轉(zhuǎn)換回數(shù)組...。最終代碼將是:


let set = new Set(arrData);

set.delete(null);

let distinctArr = [...set];


查看完整回答
反對 回復(fù) 2023-06-29
?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

將此函數(shù)添加到您的代碼中:


function removeCommonValues(arr) {

  let result = [];

  for(let i=0; i < arr.length-1; ++i) {

    if(result.includes(arr[i]) === false && arr[i] !== null)

      result.push(arr[i])

  }

  return result

}

用法:


removeCommonValues(["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]) // Return ["40-25", "50-48", "30-25", "40-23", "40-45", "40-50"]



查看完整回答
反對 回復(fù) 2023-06-29
?
慕神8447489

TA貢獻1780條經(jīng)驗 獲得超1個贊

該函數(shù)可以位于單獨的文件中,以便在多個頁面之間重用。然后您可以調(diào)用該函數(shù)來過濾不為空的不同值。


var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];


function fn(value,index,self){

return self.indexOf(value) === index && value;

 }


console.log(arrData.filter(fn));


查看完整回答
反對 回復(fù) 2023-06-29
  • 4 回答
  • 0 關(guān)注
  • 257 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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