[Ts][Js] array & Object array Distinct

**字串阵列 Distinct **

const array = ['1','2','1','3'];const setType = new Set(array);             // {'1','2','3'}const arrayType = Array.from(setType);   // ['1','2','3']

**物件阵列 Distinct **

使用Spread syntax

const arrayObjects = [{ id: '1', name: 'barry' }, { id: '3', name: 'collin' }, { id: '1', name: 'allen' }];const setType = new Set(arrayObjects.map(item => item.id)); // {'1','3'}    const arrayType = [...setType];   // ['1','3']

参考
Spread syntax:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章