JavaScript如何获取数组唯一值
获取数组唯一值
ES6 提供了从数组中提取惟一值的两种非常简洁的方法。不幸的是,它们不能很好地处理非基本类型的数组。在本文中,主要关注基本数据类型。
const cars = [
'Mazda',
'Ford',
'Renault',
'Opel',
'Mazda']const uniqueWithArrayFrom = Array.from(new Set(cars));console.log(uniqueWithArrayFrom); // outputs ["Mazda", "Ford", "Renault", "Opel"]const uniqueWithSpreadOperator = [...new Set(cars)];console.log(uniqueWithSpreadOperator);// outputs ["Mazda", "Ford", "Renault", "Opel"]
本文标题:JavaScript如何获取数组唯一值
本文链接:https://www.qqooo.cn/post/6462.html
版权说明:网站文章均来源于手工整理和网友投稿,若有不妥之处请来信 xsds@vip.qq.com 处理,谢谢!