Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // return an array with no duplicates *improved
- 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.includes(item) === false
- ? 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