PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/_octopress/source/functions/array_unique/_comments.html

https://github.com/evangelia-mitsopoulou/phpjs
HTML | 364 lines | 291 code | 72 blank | 1 comment | 0 complexity | d573b6ee063e3f0c40a4d698af421aeb MD5 | raw file
  1. <!-- Generated by Rakefile:build -->
  2. <strong>
  3. Dj
  4. </strong>
  5. on 2011-12-27 02:12:16 <br />
  6. I suggest you my version which has two main features compared with your version:
  7. First, its speed; Instead of use array_search which needs to iterate over the collection for each item of the source value, I add the new values as keys (implicity converted to strings for comparision) to a temporary object, then quickly you can check if it was added using hasOwnProperty.
  8. Second, a second paramemeter which add the feature of returning the result as array when the source value is an array.
  9. Note that in this case the array will be re-indexed because in js the keys of the arrays are numeric and needs to be continued.
  10. <pre><code>
  11. function array_unique(inputArr, preserveArray) {
  12. var key = '',
  13. val = '',
  14. hashT = {},
  15. result;
  16. if (preserveArray &amp;&amp; (inputArr instanceof Array)) {
  17. result = [];
  18. var length = inputArr &amp;&amp; inputArr.length &gt;&gt; 0;
  19. for (key = 0; key &lt; length; ++key) {
  20. val = inputArr[key];
  21. if (!hashT.hasOwnProperty(val)) {
  22. result.push(val);
  23. hashT[val] = true;
  24. }
  25. }
  26. } else {
  27. result = {};
  28. if (!inputArr) {
  29. return result;
  30. }
  31. for (key in inputArr) {
  32. val = inputArr[key];
  33. if (!hashT.hasOwnProperty(val)) {
  34. result[key] = val;
  35. hashT[val] = true;
  36. }
  37. }
  38. }
  39. return result;
  40. }
  41. </code></pre>
  42. <hr />
  43. <strong>
  44. </strong>
  45. on 2010-06-09 21:10:58 <br />
  46. @t: Associative arrays aren't native to JavaScript, but JavaScript objects share similarities to PHP associative arrays, so to try and support associative arrays, the functions on PHP.JS convert JavaScript arrays to JavaScript objects.
  47. <hr />
  48. <strong>
  49. t
  50. </strong>
  51. on 2010-06-08 19:50:09 <br />
  52. Why does this function return an object where as the php version returns an array?
  53. Thanks
  54. <hr />
  55. <strong>
  56. <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
  57. </strong>
  58. on 2010-03-19 14:55:36 <br />
  59. But to make clear, it wasn't the position that was the problem, but the lack of an argument...Dumb oversight on my part...
  60. <hr />
  61. <strong>
  62. <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
  63. </strong>
  64. on 2010-03-19 14:53:14 <br />
  65. @harald: Sorry, fixed in git: http://github.com/kvz/phpjs/raw/master/functions/array/array_unique.js . See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/hasOwnProperty for an explanation of the method. It is used to make sure we are only iterating over the immediate properties of the object, and not any that may have been added on an inherited class or on the Object/Array prototype itself...
  66. <hr />
  67. <strong>
  68. harald
  69. </strong>
  70. on 2010-03-19 12:36:02 <br />
  71. hello,
  72. may i ask:
  73. lines 25 and 35
  74. why are you testing for hasOwnProperty inside the loop and not outside?
  75. <hr />
  76. <strong>
  77. <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
  78. </strong>
  79. on 2010-02-04 02:17:15 <br />
  80. @nitin gupta: Thanks--I've fixed it in Git. I think it is a relic of the fact that we noticed that we may still _need_ to sort according to the docs, but I'm a bit busy to look at fixing this now myself.
  81. <hr />
  82. <strong>
  83. <a href="http://publicmind.in/blog" rel="nofollow">nitin gupta</a>
  84. </strong>
  85. on 2010-02-02 12:43:44 <br />
  86. Hi Kevin,
  87. I do not see this function being dependent on asort, may be a little documentation error.
  88. Regards,
  89. <hr />
  90. <strong>
  91. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  92. </strong>
  93. on 2009-04-14 12:41:42 <br />
  94. Great work guys, I'll deploy shortly
  95. <hr />
  96. <strong>
  97. <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a>
  98. </strong>
  99. on 2009-04-14 03:23:36 <br />
  100. Ok, I made the strict fix, Michael, in SVN, thank you!
  101. However, I'm not sure about the array-to-objects issue. Although we can get an array to return, due to the nature of JS, it will only contain the numerically indexed items within the array (and the length will be the total of those only). The other items would be added as properties of that array's object, and not be counted with length, though they are iteratable. Kevin has chosen to implement PHP arrays as regular (non-array) objects, so that we can support associative arrays. Granted we could theoretically return arrays with object properties, but that might be more confusing, and perhaps is the reason he chose not to do that.
  102. One solution, might be to detect whether the input array was indeed a genuine array (which could only be numerically indexed), and if so, build a bona fide array as output too, but the problem with that is that the PHP behavior is to preserve keys, and we cannot preserve keys unless we build an object (or delete/cause to be undefined, those items which are no longer in use, but that still keeps the array length)--the one rare exception where we could safely return a regular array would be if all of the unique items were at the front of the array... So, not any good answers, I suspect, though I imagine you personally should be able to adapt the output of our function to the form you need for Greasemonkey... Best wishes, Brett
  103. <hr />
  104. <strong>
  105. Michael Grier
  106. </strong>
  107. on 2009-04-13 19:03:58 <br />
  108. I found out if you're using this in GreaseMonkey, objects won't return. I changed the tmp_arr declarations to array, and it started working for me.
  109. Also, regarding the strict var, PHP always does a strict comparison after casting the arguments to strings. So you could do the following:
  110. <pre><code>
  111. if ((haystack[fkey] + &quot;&quot;) === (needle + &quot;&quot;)) {
  112. return fkey;
  113. }
  114. </code></pre>
  115. <hr />
  116. <strong>
  117. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  118. </strong>
  119. on 2008-11-09 13:03:39 <br />
  120. @ Nate: Thank you for noticing!
  121. <hr />
  122. <strong>
  123. Nate
  124. </strong>
  125. on 2008-11-06 15:46:01 <br />
  126. It looks like someone forgot to declare &amp;quot;val&amp;quot; with var.
  127. I think the first line of code should read:
  128. <pre><code>
  129. var key = '', tmp_arr1 = {}, tmp_arr2 = {}, val;
  130. </code></pre>
  131. Otherwise, &amp;quot;val&amp;quot; is a global variable, I believe.
  132. Is there any benefit to using var in a for loop? Would it be better to also declare &amp;quot;fkey&amp;quot; with &amp;quot;strict&amp;quot;?
  133. Also, I don't know if this is important, but there is no space between &amp;quot;for&amp;quot; and &amp;quot;if&amp;quot; and the opening parentheses in __array_search().
  134. <hr />
  135. <strong>
  136. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  137. </strong>
  138. on 2008-08-27 17:01:44 <br />
  139. @ Nosredna: That's an idea that calls for a function rewrite. Interested? ;)
  140. <hr />
  141. <strong>
  142. Nosredna
  143. </strong>
  144. on 2008-08-05 00:23:36 <br />
  145. Stray thought. The nested loops make me wonder how slow this is for large arrays.
  146. Perhaps you could clone the array, sort the clone, then walk the two arrays deleting the dupes as you go. Sort is probably O(n log n) and maybe it's especially fast when no user sort evaluation routine is passed in.
  147. <hr />
  148. <strong>
  149. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  150. </strong>
  151. on 2008-07-25 08:47:53 <br />
  152. @ sankai: Thank you so much! But this array_unique does not support associative arrays (javascript objects). So unlike PHP's implementation, arrays that have non-numeric keys do not work.
  153. Sad to say that my implementation didn't support it either, but I'm looking to replace array_unique with one that does support:
  154. <pre><code>
  155. {firstname: 'Kevin', surname: 'van Zonneveld'}
  156. </code></pre>
  157. .. style arrays
  158. <hr />
  159. <strong>
  160. sankai
  161. </strong>
  162. on 2008-07-25 08:21:01 <br />
  163. the code I posted yesterday had big bug!I fixed it as the following:
  164. <pre><code>
  165. function array_unique(array,numeric){
  166. // http://blog.doublekai.org/
  167. // + original by: sankai (http://blog.doublekai.org/)
  168. // * example 1: array_unique([1,2,3,'1','2','3',1,2,3],true);
  169. // * returns 1: [1,2,3]
  170. // * example 2: array_unique([1,2,3,'1','2','3',1,2,3]);
  171. // * returns 2: [1,2,3,'1','2','3']
  172. // * example 3: array_unique([1,'a','a','1','3',3,'b','c','b',1]);
  173. // * returns 3: [1,'a','1','3',3,'b','c']
  174. // * example 3: array_unique([1,'a','a','1','3',3,'b','c','b',1],true);
  175. // * returns 3: [1,'a',3,'b','c']
  176. // * NOTE :require function in_array()
  177. var tem_arr = new Array();
  178. for(i=0;i&amp;lt;array.length;i++){
  179. if(numeric === true &amp;amp;&amp;amp; typeof(array[i]) == 'string' &amp;amp;&amp;amp; !isNaN(array[i])){
  180. array[i]=parseInt(array[i],10);
  181. }
  182. if(!in_array(array[i],tem_arr)){
  183. tem_arr[i]=array[i];
  184. }
  185. }
  186. return tem_arr.join(' ').replace(/\s{2,}/g,' ').split(' ');
  187. }
  188. </code></pre>
  189. The version need require function in_array
  190. <pre><code>
  191. function array_unique(array,numeric){
  192. // http://blog.doublekai.org/
  193. // + original by: sankai (http://blog.doublekai.org/)
  194. // * example 1: array_unique([1,2,3,'1','2','3',1,2,3],true);
  195. // * returns 1: [1,2,3]
  196. // * example 2: array_unique([1,2,3,'1','2','3',1,2,3]);
  197. // * returns 2: [1,2,3,'1','2','3']
  198. // * example 3: array_unique([1,'a','a','1','3',3,'b','c','b',1]);
  199. // * returns 3: [1,'a','1','3',3,'b','c']
  200. // * example 3: array_unique([1,'a','a','1','3',3,'b','c','b',1],true);
  201. // * returns 3: [1,'a',3,'b','c']
  202. // * DON'T NEED require function in_array()
  203. var tem_arr = new Array();
  204. for(i=0;i&amp;lt;array.length;i++){
  205. if(numeric === true &amp;amp;&amp;amp; typeof(array[i]) == 'string' &amp;amp;&amp;amp; !isNaN(array[i])){
  206. array[i]=parseInt(array[i],10);
  207. }
  208. if(tem_arr.length == 0){
  209. tem_arr[i] = array[i];
  210. } else {
  211. var exist = false;
  212. for(var j=0; j&amp;lt;tem_arr.length; j++){
  213. if(tem_arr[j] === array[i]){
  214. exist = true;
  215. }
  216. }
  217. if(!exist){
  218. if(array[i] != null){
  219. tem_arr[i] = array[i];
  220. }
  221. }
  222. }
  223. }
  224. return tem_arr.join(' ').replace(/\s{2,}/g,' ').split(' ');
  225. }
  226. </code></pre>
  227. The version don't need require funciton in_array()
  228. <hr />
  229. <strong>
  230. sankai
  231. </strong>
  232. on 2008-07-24 10:05:47 <br />
  233. Hi,I try to write a code for array_unique() as the following:
  234. <pre><code>
  235. function array_unique(array){
  236. var tem_arr = new Array();
  237. for(i=0;i&amp;lt;array.length;i++){
  238. if(!in_array(array[i],tem_arr)){
  239. tem_arr[i]=array[i];
  240. }
  241. }
  242. return tem_arr.join(',').split(',');
  243. }
  244. </code></pre>
  245. NOTE:It's require function in_array()
  246. <hr />
  247. <strong>
  248. sankai
  249. </strong>
  250. on 2008-07-24 09:47:36 <br />
  251. Running:
  252. <pre><code>
  253. array_unique(['Kevin','Kevin','van','Kevin']);
  254. </code></pre>
  255. Return:
  256. <pre><code>
  257. ['Kevin','van','Kevin']
  258. </code></pre>
  259. It's the same problem as @goshki ?
  260. <hr />
  261. <strong>
  262. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  263. </strong>
  264. on 2008-06-16 00:16:50 <br />
  265. @ goshki: You're right, it seems that our current version doesn't support associative arrays (javascript objects). Since PHP doesn't distinct those, we need to work on a version that does support objects. It's on my todo list. Thank your for noticing!
  266. <hr />
  267. <strong>
  268. goshki
  269. </strong>
  270. on 2008-06-13 19:45:26 <br />
  271. Well, there seems to be some kind of a problem with this function. Running:
  272. <pre><code>
  273. array_unique(['a','b','c','a','b','c','a','b','c']);
  274. </code></pre>
  275. returns:
  276. <pre><code>
  277. ['a','a','b','c']
  278. </code></pre>
  279. <hr />
  280. <strong>
  281. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  282. </strong>
  283. on 2008-05-02 11:34:44 <br />
  284. @ duncan: You were not being stupid, we were. Thank you for noticing! The fixed version will be visible shortly.
  285. <hr />
  286. <strong>
  287. duncan
  288. </strong>
  289. on 2008-04-30 12:27:53 <br />
  290. maybe i'm being stupid, but PHP array_unique returns an array, not a boolean.
  291. &amp;quot;Takes an input array and returns a new array without duplicate values.&amp;quot;
  292. <hr />