How to do an array search in javascript?

Possible duplicate:
The easiest way to find duplicate values ​​in a JavaScript array

I am looking to determine if two values ​​in an array match. I wrote the following code:

function validatePassTimeFields(passtimes) { var success = true; var length = passtimes.length; var hashMap = new Object(); for (var j=0; j<length; j++) { if(hashMap[passtimes[j].value]==1) { success = false; alert("Duplicate Found"); break; } hashMap[passtimes[j].value]=1; } return success; } 

I am new to Javascript, so I tried using HashMap to find if there is a duplicate. Is this the best way to find duplicate in JavaScript? or can i optimize it?

+8
javascript
source share
4 answers

Your function is already very good, except for the problem that it only works for arrays with strings or numbers. For a more sophisticated approach to care, also about objects, see this answer . I do not think this is important for you, since you have an explicit and limited use case (verification of identity using the value property).

However, I would do some things differently:

  • Do not use the success and break variable from the loop, but simply return from the entire function.
  • Instead of the new Object constructor, the label literal {} usually used
  • Instead of setting values ​​in hashMap - 1 , you can use true ; you can also omit the equality operator == and just check the likelihood of the property. I would even use the in operator.
 function validatePassTimeFields(passtimes) { var length = passtimes.length; var hashMap = {}; for (var j=0; j<length; j++) { if (passtimes[j].value in hashMap) { alert("Duplicate Found"); return false; } hashMap[passtimes[j].value] = 1; } return true; } 
+1
source share

// You only need to optimize it if you want to use it elsewhere -

 function noduplicates(array){ var next, O= {}, L= array.length; while(L){ next= array[--L]; if(O[next]) return false; O[next]= 1; } return true; } function validatePassTimeFields(passtimes){ if (noduplicates(passtimes)) return true; alert("Duplicate Found"); return false; } 
+1
source share

It seems that you do not want to find duplicates, only to find out if they are?

You are pretty close, the function works here

 var hasDuplicates = function (arr) { var _store = {}; for (var i = 0; i < arr.length; i++) { if (typeof _store["_" + arr[i]] !== "undefined") { return true; } _store["_" + arr[i]] = true; } return false; }; 

Underscore characters in an associative array are needed to store numeric values. The hasDuplicates() function only works with objects that have the toString() method.

Check for duplicates;

 var yourArray = [1, 5, 7, 3, 5, 6]; if (hasDuplicates(yourArray)) {... 
0
source share

It might be worth checking out the implementation of this function by underlining. If you just want to fix errors, you can use _. Uniq () , but if you're more interested in just knowing that there are tricksters or pure implementation details, you might like the source of this method , which is very well documented.

I know that this is not a direct answer to the question - there are already several here, so it would be inappropriate to repeat. But I thought it worth mentioning that underscores are a great utility library, and the source is a great place to learn more about well-written javascript.

0
source share

All Articles