iT邦幫忙

0

[Ts][Js] array & Object array Distinct

ts js

**字串陣列 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


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言