Home > Enterprise >  FIlter out specific keys with values from JSON in Javascript?
FIlter out specific keys with values from JSON in Javascript?

Time:09-30

I'm trying to create a dynamically generating table in javascript and I have a JSON that I need to filter specific information out of

JSON to pull from (courseList)

let courseList = [
        {
            "Line": 81,
            "Department": "BUS",
            "Number": 344,
            "Section": 1,
            "Title": "MANAGEMENT OF INFORMATION SYSTEMS",
            "Faculty": "Smith, John",
            "Openings": 2,
            "Capacity": 30,
            "Status": "Open",
            "Day": "MWF",
            "StartTime": "1:25:00 PM",
            "EndTime": "2:20 PM",
            "Campus": " Main Campus",
            "Building": " Science and Engineering",
            "Room": " SE 341 Computer Science Lab",
            "Credits": 3,
            "Start Date": "8\/30\/2021",
            "End Date": "12\/17\/2021\r\n"
        },
        {
            "Line": 167,
            "Department": "CSC",
            "Number": 133,
            "Section": 2,
            "Title": "SURVEY OF COMPUTER SCIENCE",
            "Faculty": "Scott, John",
            "Openings": 6,
            "Capacity": 15,
            "Status": "Open",
            "Day": "H",
            "StartTime": "2:00:00 PM",
            "EndTime": "4:50 PM",
            "Campus": " Main Campus",
            "Building": " Science and Engineering",
            "Room": " SE 341 Computer Science Lab",
            "Credits": 0,
            "Start Date": "8\/30\/2021",
            "End Date": "12\/17\/2021\r\n"
        }
    ]

And I would like to run some kind of method to generate another JSON from the existing one. To look something like this

Expected result

let filteredCourses = [
        {
            "Title": "MANAGEMENT OF INFORMATION SYSTEMS",
            "Faculty": "Smith, John",
            "Openings": 2,
        },
        {
            "Title": "SURVEY OF COMPUTER SCIENCE",
            "Faculty": "Scott, John",
            "Openings": 6,
        }
    ]

I'm still new to javascript but I know there is a filter method on arrays that I have used, but that only gave me the keys or the values from my coursesList. I have also tried using the push method without much luck.

Is there some way to either filter out the specific key and value pairs that I need directly into a JSON, or can I filter out both the keys and values separately and combine them? I'm not sure what the best method would be.

CodePudding user response:

The filter function is used to filter elements out of an array based on a function you define.

For example, if you want to filter out all courses where there are no openings, you'd use

const filteredCourse = courseList.filter(a => a.Openings > 0);

But what you want to do is perform an operation on each element. For that you want to use map. Like this:

const filteredList = courseList.map(a => ({ Title: a.Title, Faculty: a.Faculty, Openings: a.Openings }));

Here's a snippet:

const courseList = [
    {
        "Line": 81,
        "Department": "BUS",
        "Number": 344,
        "Section": 1,
        "Title": "MANAGEMENT OF INFORMATION SYSTEMS",
        "Faculty": "Smith, John",
        "Openings": 2,
        "Capacity": 30,
        "Status": "Open",
        "Day": "MWF",
        "StartTime": "1:25:00 PM",
        "EndTime": "2:20 PM",
        "Campus": " Main Campus",
        "Building": " Science and Engineering",
        "Room": " SE 341 Computer Science Lab",
        "Credits": 3,
        "Start Date": "8\/30\/2021",
        "End Date": "12\/17\/2021\r\n"
    },
    {
        "Line": 167,
        "Department": "CSC",
        "Number": 133,
        "Section": 2,
        "Title": "SURVEY OF COMPUTER SCIENCE",
        "Faculty": "Scott, John",
        "Openings": 6,
        "Capacity": 15,
        "Status": "Open",
        "Day": "H",
        "StartTime": "2:00:00 PM",
        "EndTime": "4:50 PM",
        "Campus": " Main Campus",
        "Building": " Science and Engineering",
        "Room": " SE 341 Computer Science Lab",
        "Credits": 0,
        "Start Date": "8\/30\/2021",
        "End Date": "12\/17\/2021\r\n"
    }
]
    
const filteredList = courseList.map(a => ({ Title: a.Title, Faculty: a.Faculty, Openings: a.Openings }));
 
console.log(filteredList);

Read up on map() filter() and reduce(). They're really useful functions, if somewhat hard to wrap one's head around sometimes.

CodePudding user response:

You can just use .filter():

let filteredCourses = courseList.filter(course => ({
    Title: course.Title,
    Faculty: course.Faculty,
    Openings: course.Openings
}));

Don't confuse JS objects and JSON with each other. JSON is just an textual representation of the actual object. If needed, you can just convert the resulting filteredCourses to JSON using JSON.stringify(filteredCourses).

  • Related