Created
September 5, 2019 06:02
-
-
Save orahul1/71c34a2f24e5c7ec5658b4d42ab31bec to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/kadunomiwi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <script id="jsbin-javascript"> | |
| //Remove array duplicates | |
| "use strict"; | |
| function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
| arr = ["one", "two", "three", "four", "five", "five", "three", "two"]; | |
| //Using set | |
| var setResult = [].concat(_toConsumableArray(new Set(arr))); | |
| console.log(setResult); | |
| //using filter | |
| var removeDuplicates = function removeDuplicates(array) { | |
| return array.filter(function (item, index) { | |
| return array.indexOf(item) === index; | |
| }); | |
| }; | |
| console.log(removeDuplicates(arr)); | |
| //using forEach | |
| var removeDups = function removeDups(array) { | |
| var unique = {}; | |
| array.forEach(function (item) { | |
| if (!unique[item]) { | |
| unique[item] = true; | |
| } | |
| }); | |
| return Object.keys(unique); | |
| }; | |
| console.log(removeDups(arr)); | |
| </script> | |
| <script id="jsbin-source-javascript" type="text/javascript">//Remove array duplicates | |
| arr = ["one","two","three","four","five","five","three","two"]; | |
| //Using set | |
| let setResult = [... new Set(arr)]; | |
| console.log(setResult); | |
| //using filter | |
| let removeDuplicates = (array) => array.filter((item,index) => array.indexOf(item) === index); | |
| console.log(removeDuplicates(arr)); | |
| //using forEach | |
| let removeDups = (array) => { | |
| let unique = {}; | |
| array.forEach((item) => { | |
| if(!unique[item]){ | |
| unique[item] = true; | |
| } | |
| }); | |
| return Object.keys(unique); | |
| } | |
| console.log(removeDups(arr)); | |
| </script></body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Remove array duplicates | |
| "use strict"; | |
| function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
| arr = ["one", "two", "three", "four", "five", "five", "three", "two"]; | |
| //Using set | |
| var setResult = [].concat(_toConsumableArray(new Set(arr))); | |
| console.log(setResult); | |
| //using filter | |
| var removeDuplicates = function removeDuplicates(array) { | |
| return array.filter(function (item, index) { | |
| return array.indexOf(item) === index; | |
| }); | |
| }; | |
| console.log(removeDuplicates(arr)); | |
| //using forEach | |
| var removeDups = function removeDups(array) { | |
| var unique = {}; | |
| array.forEach(function (item) { | |
| if (!unique[item]) { | |
| unique[item] = true; | |
| } | |
| }); | |
| return Object.keys(unique); | |
| }; | |
| console.log(removeDups(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment