5 examples of 'javascript unique array of objects' in JavaScript

Every line of 'javascript unique array of objects' 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
23function unique(array){
24 var o = {};
25 for(var i=0; i<array.length; i++){
26 var item = array[i];
27 //判断字典中是否已经存在该值
28 //如果不存在 = 1
29 //如果存在 + 1
30 if(o[item]){
31 o[item]++;
32 }else{
33 o[item] = 1;
34 }
35 }
36 // 删掉原来的数组
37 array = null;
38 // 去重后的新数组
39 var newArray = [];
40 for(var key in o) {
41 newArray.push(key);
42 }
43 return newArray;
44}
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

33function unique (array) {
34 var res = []
35
36 for(var i = 0,iL = array.length;i < iL;i++) {
37 var current = array[i]
38 if(res.indexOf(current) === -1) {
39 res.push(current)
40 }
41 }
42
43 return res
44}
19function unique(array) {
20 var n = -1;
21
22 while (++n < array.length) {
23 remove(array, array[n], n);
24 }
25
26 return array;
27}
104function arrayUniqueTest() {
105 // var target = [1, 2, 3, 3, '3', '3', 'length', '__proto__', 'prototype', true, false, true, {}, {}, null, null];
106 var target = [1, 2, 3, 3, '3', '3', '__proto__', '__proto__', '__proto__', 'prototype', 'prototype', true, false, true, {}, {}, null, null];
107 // var target = [1, '1', true, 'true'];
108 console.log('\narrayUnique test:\n', arrayUnique(target));
109}
103function arrayUnique(array) {
104 const a = array.concat();
105 for (let i = 0; i < a.length; ++i) {
106 for (let j = i + 1; j < a.length; ++j) {
107 if (a[i] === a[j]) {
108 a.splice(j--, 1);
109 }
110 }
111 }
112 return a;
113}

Related snippets