Created
April 20, 2018 07:18
-
-
Save 6ui11em/f759b9ce6a465a683ea560e263c6683f to your computer and use it in GitHub Desktop.
Javascript remove duplicates from array #javascript #array
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
var data = [ | |
{ | |
name: 'Kyle', | |
occupation: 'Fashion Designer' | |
}, | |
{ | |
name: 'Liza', | |
occupation: 'Web Developer' | |
}, | |
{ | |
name: 'Emily', | |
occupation: 'Web Designer' | |
}, | |
{ | |
name: 'Melissa', | |
occupation: 'Fashion Designer' | |
}, | |
{ | |
name: 'Tom', | |
occupation: 'Web Developer' | |
} | |
]; | |
// Array Map | |
var jobs = data.map(function (item) { | |
return item.occupation; | |
}); | |
// Logs ["Fashion Designer", "Web Developer", "Web Designer", "Fashion Designer", "Web Developer"] | |
console.log(jobs); | |
// Array Filter | |
var jobsUnique = jobs.filter(function(item, index){ | |
return jobs.indexOf(item) >= index; | |
}); | |
// Logs ["Fashion Designer", "Web Developer", "Web Designer"] | |
console.log(jobsUnique); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment