[Vuejs]-Unsure what this code is doing? I want it to do the opposite of what it's actually doing

1👍

In the second line the filter method loops all items in this.courses and returns only those items where the statement inside returns true. indexOf is an array method that searches an array for the specified item and returns the position of the item in the array or -1 if the items isn’t found. so I guess what you want to do is filter for courses where indexOf is greater or equals than/to 0, instead of less, here is your code modified.

addCourses() {
    const currentCourses = this.packageForm.package_courses.map((item) => item.course_id);
    const courses = this.courses.filter((item) => {
        return this.selectedCourses.indexOf(item.id) && currentCourses.indexOf(item.id) >= 0
    });

    courses.forEach((course) => {
        this.packageForm.package_courses.push({
            course_id: course.id,
            course: course,
            price: 0
        });
    });
    this.selectedCourses = [];
},

Leave a comment