1

I have a data set that looks as such:

const data = [{
    "First Name" : "John"
    "Last Name": "Doe"
    "Cars Sold": 2
  },
  {
    "First Name" : "John"
    "Last Name": "Doe"
    "Cars Sold": 1
  },
  {
    "First Name" : "Jane"
    "Last Name": "Doe"
    "Cars Sold": 3
  }];

How would i go about filtering this to get a list of all the people in the object? i.e. the output should be a list of unique people (combination of first and last name) in the object.

Thanks

adiga
  • 34,372
  • 9
  • 61
  • 83
Jonas Bæk
  • 100
  • 9
  • what does not work? – Nina Scholz Jun 18 '19 at 11:03
  • Thanks for answering Nina. The duplicate question you're referring to doesn't quite solve my issue. Those solutions are only filtering on 1 parameter, while i would like to filter on a combination of two parameters: First Name & Last Name. The dataset contains only two people, but if i filter based on Last Name it would return just 1 name. – Jonas Bæk Jun 18 '19 at 11:09

2 Answers2

4

Unique people, with first and last name separated by a space:

const data = [{"First Name":"John", "Last Name":"Doe", "Cars Sold":2},{"First Name":"John", "Last Name":"Doe", "Cars Sold":1},{"First Name":"Jane", "Last Name":"Doe", "Cars Sold":3}];
const res = [...new Set(data.map(e => e["First Name"] + " " + e["Last Name"]))];
console.log(res);

ES5 syntax:

var data = [{"First Name":"John", "Last Name":"Doe", "Cars Sold":2},{"First Name":"John", "Last Name":"Doe", "Cars Sold":1},{"First Name":"Jane", "Last Name":"Doe", "Cars Sold":3}];
var res = data.map(function(e) {
  return e["First Name"] + " " + e["Last Name"];
}).filter(function(e, i, a) {
  return a.indexOf(e) == i;
});
console.log(res);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
2

const data = [{
    "First Name" : "John",
    "Last Name": "Doe",
    "Cars Sold": 2
  },
  {
    "First Name" : "John",
    "Last Name": "Doe",
    "Cars Sold": 1
  },
  {
    "First Name" : "Jane",
    "Last Name": "Doe",
    "Cars Sold": 3
  }];

const list = data.reduce((acc, cur) => acc.includes(`${cur['First Name']} ${cur['Last Name']}`) ? acc : acc.concat([`${cur['First Name']} ${cur['Last Name']}`]), []);

console.log(list);
Mamun
  • 66,969
  • 9
  • 47
  • 59
MorKadosh
  • 5,846
  • 3
  • 25
  • 37