Monday, 20 March 2017

Getting unique values from an array of objects in Javascript

In my previous post I looked at using JSFiddle to process data from a MySQL database into DynamoDB. One of the things I fumbled with was extracting the unique dates from the array of objects:

let attendance = [{
    "date": "2015-01-07",
    "username": "Others",
    "name": 8,
    "dues": null
}, {
    "date": "2015-01-14",
    "username": "Others",
    "name": 10,
    "dues": null
}, {
    "date": "2015-01-21",
    "username": "Others",
    "name": 9,
    "dues": null
}, {
    "date": "2015-01-28",
    "username": "Others",
    "name": 8,
    "dues": null
}, {
    "date": "2015-02-04",
    "username": "Others",
    "name": 8,
    "dues": null
}, {
    "date": "2015-12-09",
    "username": "Others",
    "name": 9,
    "dues": null
}, {
    "date": "2015-12-16",
    "username": "Others",
    "name": 9,
    "dues": null
}, {
    "date": "2015-12-23",
    "username": "Others",
    "name": 8,
    "dues": null
}, {
    "date": "2015-12-30",
    "username": "Others",
    "name": 7,
    "dues": null
}, {
    "date": "2016-01-13",
    "username": "user1@example.com",
    "name": "Dominic",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user2@example.com",
    "name": "Derek",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user3@example.com",
    "name": "Gemma",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user5@example.com",
    "name": "Chris",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user6+user2@example.com",
    "name": "Jack",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user4@example.com",
    "name": "James",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user7@example.com",
    "name": "Peter",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user8@example.com",
    "name": "Mariusz",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user9@example.com",
    "name": "Patrick",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user11@example.com",
    "name": "Russell",
    "dues": 1
}, {
    "date": "2016-01-13",
    "username": "user10@example.com",
    "name": "Sarah",
    "dues": 1
}, {
    "date": "2016-01-17",
    "username": "user1@example.com",
    "name": "Dominic",
    "dues": 1
}, {
    "date": "2016-01-17",
    "username": "user2@example.com",
    "name": "Derek",
    "dues": 0
}, {
    "date": "2016-01-17",
    "username": "user3@example.com",
    "name": "Gemma",
    "dues": 0
}, {
    "date": "2016-01-17",
    "username": "user5@example.com",
    "name": "Chris",
    "dues": 0
}, {
    "date": "2016-01-17",
    "username": "user6+user2@example.com",
    "name": "Jack",
    "dues": 0
}, {
    "date": "2016-01-17",
    "username": "user7@example.com",
    "name": "Peter",
    "dues": 0
}, {
    "date": "2016-01-19",
    "username": "user1@example.com",
    "name": "Dominic",
    "dues": 0
}, {
    "date": "2016-01-19",
    "username": "user2@example.com",
    "name": "Derek",
    "dues": 0
}, {
    "date": "2016-01-19",
    "username": "user3@example.com",
    "name": "Gemma",
    "dues": 0
}, {
    "date": "2016-01-19",
    "username": "user4@example.com",
    "name": "James",
    "dues": 0
}];

While I was looking at this process I came across this answer on Stack Overflow:

const dates = [...new Set(attendance.map(item => item.date))];

It's brilliant! So brilliant that I'm putting it here so I don't forget (working JSFiddle)!