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

2
  • what does not work? Commented Jun 18, 2019 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. Commented Jun 18, 2019 at 11:09

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.