PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/website/source/php/array/array_unique.html

https://github.com/andriuspetrauskis/phpjs
HTML | 89 lines | 85 code | 4 blank | 0 comment | 0 complexity | 7f545a4f5005a479869c7f4810df0545 MD5 | raw file
  1. ---
  2. warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand'
  3. examples:
  4. - 'array_unique([''Kevin'',''Kevin'',''van'',''Zonneveld'',''Kevin''])'
  5. - 'array_unique({''a'': ''green'', 0: ''red'', ''b'': ''green'', 1: ''blue'', 2: ''red''})'
  6. estarget: es5
  7. returns:
  8. - '{0: ''Kevin'', 2: ''van'', 3: ''Zonneveld''}'
  9. - '{a: ''green'', 0: ''red'', 1: ''blue''}'
  10. dependencies: []
  11. authors:
  12. original by:
  13. - 'Carlos R. L. Rodrigues (http://www.jsfromhell.com)'
  14. improved by:
  15. - Michael Grier
  16. bugfixed by:
  17. - 'Kevin van Zonneveld (http://kvz.io)'
  18. - Nate
  19. - 'Kevin van Zonneveld (http://kvz.io)'
  20. - 'Brett Zamir (http://brett-zamir.me)'
  21. input by:
  22. - duncan
  23. - 'Brett Zamir (http://brett-zamir.me)'
  24. notes:
  25. - |-
  26. The second argument, sort_flags is not implemented;
  27. also should be sorted (asort?) first according to docs
  28. type: function
  29. layout: function
  30. title: PHP's array_unique in JavaScript
  31. description: >-
  32. Heres what our current JavaScript equivalent to <a
  33. href="http://php.net/manual/en/function.array_unique.php">PHP's
  34. array_unique</a> looks like.
  35. function: array_unique
  36. category: array
  37. language: php
  38. permalink: php/array/array_unique/
  39. alias:
  40. - /functions/php/array_unique/
  41. - /functions/array/array_unique/
  42. - /php/array_unique/
  43. - /functions/array_unique/
  44. ---
  45. {% codeblock lang:javascript %}module.exports = function array_unique (inputArr) { // eslint-disable-line camelcase
  46. // discuss at: http://locutus.io/php/array_unique/
  47. // original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
  48. // input by: duncan
  49. // input by: Brett Zamir (http://brett-zamir.me)
  50. // bugfixed by: Kevin van Zonneveld (http://kvz.io)
  51. // bugfixed by: Nate
  52. // bugfixed by: Kevin van Zonneveld (http://kvz.io)
  53. // bugfixed by: Brett Zamir (http://brett-zamir.me)
  54. // improved by: Michael Grier
  55. // note 1: The second argument, sort_flags is not implemented;
  56. // note 1: also should be sorted (asort?) first according to docs
  57. // example 1: array_unique(['Kevin','Kevin','van','Zonneveld','Kevin'])
  58. // returns 1: {0: 'Kevin', 2: 'van', 3: 'Zonneveld'}
  59. // example 2: array_unique({'a': 'green', 0: 'red', 'b': 'green', 1: 'blue', 2: 'red'})
  60. // returns 2: {a: 'green', 0: 'red', 1: 'blue'}
  61. var key = ''
  62. var tmpArr2 = {}
  63. var val = ''
  64. var _arraySearch = function (needle, haystack) {
  65. var fkey = ''
  66. for (fkey in haystack) {
  67. if (haystack.hasOwnProperty(fkey)) {
  68. if ((haystack[fkey] + '') === (needle + '')) {
  69. return fkey
  70. }
  71. }
  72. }
  73. return false
  74. }
  75. for (key in inputArr) {
  76. if (inputArr.hasOwnProperty(key)) {
  77. val = inputArr[key]
  78. if (_arraySearch(val, tmpArr2) === false) {
  79. tmpArr2[key] = val
  80. }
  81. }
  82. }
  83. return tmpArr2
  84. }
  85. {% endcodeblock %}