-1

I have an array of objects

[
  {"first_name" :"John", "last_name": "Smith",  "course": "course 1"}, 
  {"first_name" :"Joe", "last_name": "Doe",  "course": "course 2"}, 
  {"first_name" :"John", "last_name": "Smith",  "course": "course 3"} 
] 

How can I grab together unique first_name and last_name to get:

[
  {"first_name" :"John", "last_name": "Smith",  "course": "course 1"}, 
  {"first_name" :"Joe", "last_name": "Doe",  "course": "course 2"}
] 

in a new array?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Piotr
  • 1
  • 1
  • What have you tried, and what exactly is the problem with it? – jonrsharpe Mar 15 '18 at 16:37
  • https://stackoverflow.com/questions/1960473/get-all-unique-values-in-an-array-remove-duplicates – An0num0us Mar 15 '18 at 16:37
  • @jonrsharpe thank you for moderating my post, I'm new here and I was not able to find an answer anywhere to my problem and I was not sure how to ask a question. I solved it this way https://stackoverflow.com/a/49309805/9446440 – Piotr Mar 15 '18 at 21:53

3 Answers3

1

You can try following using Array.filter

var arr = [
  {"first_name" :"John", "last_name": "Smith",  "course": "course 1"}, 
  {"first_name" :"Joe", "last_name": "Doe",  "course": "course 2"}, 
  {"first_name" :"John", "last_name": "Smith",  "course": "course 3"} 
]; 

var map = {}; // create a map that stores unique combination of fields (first_name and last_name)

arr = arr.filter(function(item){
  if(!map[item.first_name + "_" + item.last_name]) { 
    // store the first occurrence of combination in map and ignore others
    map[item.first_name + "_" + item.last_name] = "first_unique_record";
    return true;
  }
  return false;
});

console.log(arr);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
  • Thank you for replay I solve my problem this way. stackoverflow.com/a/49309805/9446440 I'm not sure if that is the best way but it works – Piotr Mar 15 '18 at 21:55
  • @Piotr - This is also a potential solution, however, there will be some difference in performance – Nikhil Aggarwal Mar 16 '18 at 04:24
0

use lodash Lodash in its functions .filter(collection, [predicate=.identity]) or .find(collection, [predicate=.identity], [fromIndex=0])

0

I'm not sure if that is the best code but I solve my problem this way:

var dataJson = [
    {"first_name" :"John", "last_name": "Smith",  "course": "course 1"}, 
    {"first_name" :"Joe", "last_name": "Doe",  "course": "course 2"}, 
    {"first_name" :"John", "last_name": "Smith",  "course": "course 3"} 
   ];

   var tmparr = [];

   var newJson = [];
   var duplicatesJson = [];

   dataJson.forEach((value, index) => {
                let a = value.first_name + value.last_name
                        if(!(this.tmparr.includes(a))){
                        console.log('unique');             
                        this.tmparr.push(a);
                        this.newJson.push(value);
                        } else {
                        console.log('duplicate')
                        this.duplicatesJson.push(value);                            
                        }
            })

   console.log(this.newJson);
   console.log(this.duplicatesJson)
Piotr
  • 1
  • 1