0

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.

1 Answer 1

1

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.

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

1 Comment

It worked! Thank you so much, that was so much easier than all the other ways I was trying to do it. I haven't heard of map before, I'll definitely have to read on exactly how it works to make use of it later. Thanks again!

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.