如何在 javascript 中使用集合并获得相同的结果

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

结果不正确我可以用什么来区别?


一只名叫tom的猫
浏览 121回答 1
1回答

婷婷同学_

这可能就是您正在寻找的:function padHours(str) {&nbsp; &nbsp; const [minutes, seconds] = str.match(/\d{1,2}/g);&nbsp; &nbsp; 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 Setconst result = new Set(arr);console.log(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript