PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

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

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