10 examples of 'javascript remove duplicate objects from array es6' in JavaScript

Every line of 'javascript remove duplicate objects from array es6' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
52function horribleDuplicates (array) {
53 for (var i = 0; i < array.length; i++) {
54 for (var j = 0; j < array.length; j++) {
55 if (array[i] == array[j]) {
56 return true
57 }
58 }
59 }
60 return false
61}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

64function duplicateArray(array) {
65 return array.clone ? array.clone() : new Array(array.length);
66}
25function removeDuplicates_for_loop(arr) {
26 let res = [];
27 const len = arr.length;
28 for (let i = 0; i < len; i++) {
29 if (res.indexOf(arr[i]) === -1) {
30 res.push(arr[i]);
31 }
32 }
33 return res;
34}
377function removeDuplicates(arr) {
378 var ret = [], i = -1, l, retLength = 0;
379 if (arr) {
380 l = arr.length;
381 while (++i < l) {
382 var item = arr[i];
383 if (indexOf(ret, item) === -1) {
384 ret[retLength++] = item;
385 }
386 }
387 }
388 return ret;
389}
48function removeDuplicates(_array) {
49 var _uniques = [], _dupl = [], i = 0;
50 for (i; i <= _array.length; i++) {
51 var v = _array[i];
52 console.log(i + '-' + v);
53 if (_array.lastIndexOf(v) > i || _dupl.indexOf(v) > -1) {
54 _dupl.push(v);
55 } else {
56 _uniques[_uniques.length] = v;
57 }
58 }
59
60 return $.grep(_uniques, function(n, i) {
61 return (n);
62 });
63}
9public static removeDuplicates(array: any[]) {
10 if (!array) {
11 return [];
12 }
13 return array.filter((current, index) => {
14 return array.indexOf(current) === index;
15 });
16}
30function duplicateArray(arr) {
31 var newArr = [];
32 for(var i=0, arrSize=arr.length; i<arrSize; i++) {
33 newArr.push(arr[i]);
34 }
35 return newArr;
36}
38function arrayRemoveDuplicates(values, equalsEpsilon, wrapAround) {
39 //>>includeStart('debug', pragmas.debug);
40 Check.defined('equalsEpsilon', equalsEpsilon);
41 //>>includeEnd('debug');
42
43 if (!defined(values)) {
44 return undefined;
45 }
46
47 wrapAround = defaultValue(wrapAround, false);
48
49 var length = values.length;
50 if (length < 2) {
51 return values;
52 }
53
54 var i;
55 var v0;
56 var v1;
57
58 for (i = 1; i < length; ++i) {
59 v0 = values[i - 1];
60 v1 = values[i];
61 if (equalsEpsilon(v0, v1, removeDuplicatesEpsilon)) {
62 break;
63 }
64 }
65
66 if (i === length) {
67 if (wrapAround && equalsEpsilon(values[0], values[values.length - 1], removeDuplicatesEpsilon)) {
68 return values.slice(1);
69 }
70 return values;
71 }
72
73 var cleanedvalues = values.slice(0, i);
74 for (; i < length; ++i) {
75 // v0 is set by either the previous loop, or the previous clean point.
76 v1 = values[i];
77 if (!equalsEpsilon(v0, v1, removeDuplicatesEpsilon)) {
78 cleanedvalues.push(v1);
79 v0 = v1;
80 }
81 }
82
83 if (wrapAround && cleanedvalues.length > 1 && equalsEpsilon(cleanedvalues[0], cleanedvalues[cleanedvalues.length - 1], removeDuplicatesEpsilon)) {
84 cleanedvalues.shift();
85 }
86
87 return cleanedvalues;
88}
11export function removeArray(array, val, callback) {
12 var index = array.indexOf(val);
13 //如果找到
14 if (index > -1) {
15 callback && callback()
16 array.splice(index, 1);
17 }
18}
270export function getDuplicateNames(indexableArray: { name: string }[]): string[] {
271 const seen: { [name: string]: number } = {};
272 const duplicates: string[] = [];
273
274 indexableArray.forEach((element: { name: string }) => {
275 const { name } = element;
276 const count = seen[name] || 0;
277
278 if (count === 1) {
279 duplicates.push(name);
280 }
281 seen[name] = count + 1;
282 });
283 return duplicates;
284}

Related snippets