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.

1
  • 1
    Have you tried anything yourself? at all? a little? Commented May 29, 2018 at 3:52

3 Answers 3

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>

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

8 Comments

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.
ahhh never looked that close. Fix in a minute
OK reduced to set instead. Easier to split the multiples there
Thanks @charlietfl For some reason when I ran your code on my large dataset, it is skipping 1 value.
Anything wierd about that particular value within the original string?
|
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()

6 Comments

Thanks @Eddie, This is working great. Is it possible to trim the values. One of the values inside the array contains a space.
@Slyper Yes, you can use map and trim the values. I updated my answer.
Hmmmm.... For some reason when I run your code in Chrome console, I get this error ... "Uncaught SyntaxError: Identifier 'arr' has already been declared"
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
Thanks @Eddie. Really appreciate all your help. All good now.
|
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.