简体   繁体   中英

How to verify array within object

So I'm part of a bootcamp that helps you get job ready to do web development. I hit a snag and wanted to know if anyone could shed some light. inTruth('I', 'is', 'dumb'); The goal is to have the Bootstrap class's method "registerStudent", verify if there is a matching object within it's array "students" if there is, it say "there is already a student registered", if not it will add the called method and add the object to the class's array. If my explanation isn't through. runApology('I', 'am', 'sorry', * 2);

HTML

<head>
    <title>Nucamp Community Coding Bootcamp</title>
</head>
<body>
    <h1>Nucamp Community Coding Bootcamp</h1>
    <script src="ReactWeek1Assignment.js"></script>
</body>
</html>

class Student {
    constructor(name, email, community) {
        this.name = name;
        this.email = email;
        this.community = community;
    }
}
class Bootcamp {
    constructor(name, level, students = []) {
        this.name = name;
        this.level = level;
        this.students = students;
    }
        registerStudent(studentToRegister) {
                if (this.students.filter(s => s === studentToRegister)){
                    this.students.push(studentToRegister);
                    console.log(`Registering ${studentToRegister.name} to ${this.name} with email ${studentToRegister.email}`);
                    return this.students;
                } else {
                    console.error(`${studentToRegister.name} is not registered`);
                    return this.students;
                }
            }

        }

I would suggest to use Lodash . It has many in-built comparison functions. You should use the isEqual() for the same. Kindly refer the following code.

_.isEqual(obj1,obj2);

For further reference you can refer this documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM