3

I have a JSON array like this ...

[
  {
    "Event_code": "AB-001",
    "Interest_area": "Arts and Education",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-002",
    "Interest_area": "Arts and Education",
    "Start_time": "12:30 PM",
    "End_time": "1:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-003",
    "Interest_area": "",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-004",
    "Interest_area": "Business",
    "Start_time": "10:30 AM",
    "End_time": "11:00 AM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-005",
    "Interest_area": "General Interest",
    "Start_time": "9:30 AM",
    "End_time": "1:30 PM",
    "Session_type": "Experience"
  },
  {
    "Event_code": "AB-006",
    "Interest_area": "Environment, Business",
    "Start_time": "11:00 AM",
    "End_time": "11:30 AM",
    "Session_type": "Course information session"
  },
 {
    "Event_code": "AB-014",
    "Interest_area": "Health sciences and allied health, Medicine",
    "Start_time": "1:00 PM",
    "End_time": "2:00 PM",
    "Session_type": "Course information session"
  }
    ]

What I want to do is filter this JSON and extract unique "Interest_area" values where "Session_type" is equal to "Course information session" ...

My expected output is

["Arts and Education","Business","Environment"]

I have seen this solution, which is pretty close to what I am looking for, but it does not work in my case as my JSON may have "2 or more" values for the "Interest Area" field.

Slyper
  • 896
  • 2
  • 15
  • 32

3 Answers3

2

First start with filtering using Array#filter() to get non empty interests and appropriate session type

Array#reduce() filtered result to a Set of interests. A Set can only have unique values and ignores duplicates

Finally convert Set back to array

let interestSet = data       
  .filter(obj=> obj.Interest_area && obj.Session_type === "Course information session")   
  .reduce((a, c)=> a.add(...c.Interest_area.split(',')), new Set);
  
let uniques = [...interestSet];

console.log(uniques)
<script>
let data = [
  {
    "Event_code": "AB-001",
    "Interest_area": "Arts and Education",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-002",
    "Interest_area": "Arts and Education",
    "Start_time": "12:30 PM",
    "End_time": "1:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-003",
    "Interest_area": "",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-004",
    "Interest_area": "Business",
    "Start_time": "10:30 AM",
    "End_time": "11:00 AM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-005",
    "Interest_area": "General Interest",
    "Start_time": "9:30 AM",
    "End_time": "1:30 PM",
    "Session_type": "Experience"
  },
  {
    "Event_code": "AB-006",
    "Interest_area": "Environment,Business",
    "Start_time": "11:00 AM",
    "End_time": "11:30 AM",
    "Session_type": "Course information session"
  }
]
</script>
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Hi @charlietfl, You solution looks good, but it DOES NOT produce expected output. The last set in the array has 2 values for 'Interest_area' key. I need these values to to be sliced off into 2 separate values in the array. – Slyper May 29 '18 at 04:05
  • ahhh never looked that close. Fix in a minute – charlietfl May 29 '18 at 04:08
  • OK reduced to set instead. Easier to split the multiples there – charlietfl May 29 '18 at 04:25
  • Thanks @charlietfl For some reason when I ran your code on my large dataset, it is skipping 1 value. – Slyper May 29 '18 at 04:42
  • Anything wierd about that particular value within the original string? – charlietfl May 29 '18 at 04:43
  • No nothing particular. Eddie's code below is returning all the values on the same dataset. – Slyper May 29 '18 at 04:44
  • Run with his then. Really late for me right now. Was more an excercise in helping you get started anyway – charlietfl May 29 '18 at 04:46
  • Thanks @charlietfl, No I really appreciate all your insights. Your code is skipping the value "Medicine" from this set `'"Interest_area": "Health sciences and allied health, Medicine" – Slyper May 29 '18 at 04:48
1

You can do:

let result = [...new Set(                                              //Use new Set to get unique values
   arr.filter(o=>o.Session_type === search && o.Interest_area.trim() !== '' ) //Use filter to filter the Session_type and Interest_area is not blank
      .reduce((c,v)=>c.concat(v.Interest_area.split(',')),[]))         //Use reduce and concat to flatten the array
      .map(o=>o.trim())                                                //Use map to trim the values
]

Here is a snippet:

let arr=[{"Event_code":"AB-001","Interest_area":"Arts and Education","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Course information session"},{"Event_code":"AB-002","Interest_area":"Arts and Education","Start_time":"12:30 PM","End_time":"1:00 PM","Session_type":"Course information session"},{"Event_code":"AB-003","Interest_area":"","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Course information session"},{"Event_code":"AB-004","Interest_area":"Business","Start_time":"10:30 AM","End_time":"11:00 AM","Session_type":"Course information session"},{"Event_code":"AB-005","Interest_area":"General Interest","Start_time":"9:30 AM","End_time":"1:30 PM","Session_type":"Experience"},{"Event_code":"AB-006","Interest_area":"Environment   ,    Business       ","Start_time":"11:00 AM","End_time":"11:30 AM","Session_type":"Course information session"}];

let search = 'Course information session';
let result = [...new Set(arr.filter(o=>o.Session_type === search && o.Interest_area.trim() !== '' ).reduce((c,v)=>c.concat(v.Interest_area.split(',')),[]).map(o=>o.trim()))]

console.log(result);

Doc: new Set(), filter(), reduce(), concat()

Eddie
  • 26,593
  • 6
  • 36
  • 58
  • Thanks @Eddie, This is working great. Is it possible to trim the values. One of the values inside the array contains a space. – Slyper May 29 '18 at 04:20
  • @Slyper Yes, you can use `map` and trim the values. I updated my answer. – Eddie May 29 '18 at 04:23
  • Hmmmm.... For some reason when I run your code in Chrome console, I get this error ... "Uncaught SyntaxError: Identifier 'arr' has already been declared" – Slyper May 29 '18 at 04:26
  • Maybe you are already using the variable named `arr` and you are initiating it again with `let`. You can use a different variable name. @Slyper – Eddie May 29 '18 at 04:27
  • Thanks @Eddie. Really appreciate all your help. All good now. – Slyper May 29 '18 at 04:53
  • Happy to help :) – Eddie May 29 '18 at 04:55
0

You have to loop in each subarray and check the value of Session Type, then extract the interest area

var obj = [
  {
    "Event_code": "AB-001",
    "Interest_area": "Arts and Education",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-002",
    "Interest_area": "Arts and Education",
    "Start_time": "12:30 PM",
    "End_time": "1:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-003",
    "Interest_area": "",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-004",
    "Interest_area": "Business",
    "Start_time": "10:30 AM",
    "End_time": "11:00 AM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-005",
    "Interest_area": "General Interest",
    "Start_time": "9:30 AM",
    "End_time": "1:30 PM",
    "Session_type": "Experience"
  },
  {
    "Event_code": "AB-006",
    "Interest_area": "Environment,Business",
    "Start_time": "11:00 AM",
    "End_time": "11:30 AM",
    "Session_type": "Course information session"
  }
]

var interest_list = [];
$.each(obj, function(event,details){
  if(details['Session_type'] === 'Course information session'){
    var addnew = details['Interest_area'].split(",");
    interest_list.push(addnew);
  }
}

console.log(interest_list);