Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. let items = ['Work Order', 'workOrder', 'work order'];
  12.  
  13.  
  14. console.log(getHeaderFieldsWihoutDuplicates(items))
  15.  
  16. function getHeaderFieldsWihoutDuplicates(fieldsArray) {
  17. let newItems = [];
  18. fieldsArray.forEach(i => {
  19. var item = i.replace(/\s/g, '').toLowerCase();
  20. // create a regex for the new item
  21. let regA = new RegExp(item + '\d?');
  22. // get same item array
  23. let sameItems = newItems.filter((a)=> {
  24. if (a.match(regA)) {
  25. return a;
  26. }
  27. });
  28. // check length
  29. if (sameItems.length === 0) {
  30. // if there is no existing items then add the item
  31. newItems.push(item);
  32. } else {
  33. // if there are items then add with index prefix
  34. newItems.push(item + sameItems.length);
  35. }
  36.  
  37. });// end of forEach
  38. return newItems;
  39. }
  40. </script>
  41.  
  42.  
  43.  
  44. <script id="jsbin-source-javascript" type="text/javascript">let items = ['Work Order', 'workOrder', 'work order'];
  45.  
  46.  
  47. console.log(getHeaderFieldsWihoutDuplicates(items))
  48.  
  49. function getHeaderFieldsWihoutDuplicates(fieldsArray) {
  50. let newItems = [];
  51. fieldsArray.forEach(i => {
  52. var item = i.replace(/\s/g, '').toLowerCase();
  53. // create a regex for the new item
  54. let regA = new RegExp(item + '\d?');
  55. // get same item array
  56. let sameItems = newItems.filter((a)=> {
  57. if (a.match(regA)) {
  58. return a;
  59. }
  60. });
  61. // check length
  62. if (sameItems.length === 0) {
  63. // if there is no existing items then add the item
  64. newItems.push(item);
  65. } else {
  66. // if there are items then add with index prefix
  67. newItems.push(item + sameItems.length);
  68. }
  69.  
  70. });// end of forEach
  71. return newItems;
  72. }</script></body>
  73. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement