首页 > 解决方案 > Merge multiple object values of array into 1

问题描述

This is my array:

[ 
  'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
  'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
  'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
  'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3'
] 

If want to check, if it has similar objects then I want it to be merged into one and return me array as:

[
  'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
  'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
] 

How is this possible in JavaScript or TypeScript?

标签: javascripttypescript

解决方案


Use Set to remove duplicate values :

array = [ 'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
  'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
  'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
  'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3' ];
  
var result = [...new Set(array)];

console.log(result);


推荐阅读