亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何在 javascript 中使用集合并獲得相同的結果

如何在 javascript 中使用集合并獲得相同的結果

一只名叫tom的貓 2023-07-06 18:28:07
在 javascript 中,我有 2 個 seta,并從 A 組中刪除 B 組A 組:7,8,9,10 B 組:8,9結果應該是:7,10使用 var availableA = [...new Set([...workingA].filter(x => !b1.has(x)))];結果不正確我可以用什么來區別?
查看完整描述

1 回答

?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

這可能就是您正在尋找的:


function padHours(str) {

    const [minutes, seconds] = str.match(/\d{1,2}/g);

    return (minutes.length < 2 ? '0' + minutes : minutes) + ':' + seconds;

}


const a = new Set(['07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00'].map(padHours));

const b = new Set(['07:00', '8:00', '9:00', '15:00', '16:00', '17:00', '11:00', '12:00', '13:00'].map(padHours));


// Converting to an array allows to filter (which is not possible on Sets)


const arr = [...a, ...b].filter(x => !a.has(x) || !b.has(x));


console.log(arr);


// And if you absolutely want the result as a Set


const result = new Set(arr);


console.log(result);


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號