Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // return an array with no duplicates
- let animals = ['cat', 'dog', 'cat', 'dog'];
- let booleans = [true, false, true, false];
- let nums = [1,2,1,2];
- let mixture = ['cat', true, 1, 'cat', true, 1];
- const returnNoDuplicates = (arr) => {
- return arr.reduce((newArr, item) => {
- newArr.find(uniqueItem => uniqueItem === item) === undefined
- ? newArr.push(item)
- : null
- return newArr;
- }, [])
- };
- console.log(returnNoDuplicates(animals));
- console.log(returnNoDuplicates(booleans));
- console.log(returnNoDuplicates(nums));
- console.log(returnNoDuplicates(mixture));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement