简体   繁体   English

如何验证 object 中的阵列

[英]How to verify array within object

So I'm part of a bootcamp that helps you get job ready to do web development.因此,我参加了一个训练营,帮助您准备好进行 web 开发。 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.目标是让 Bootstrap 类的方法“registerStudent”,验证它的数组“students”中是否有匹配的 object,如果有,它说“已经有一个学生注册”,如果没有,它将添加调用的方法和将 object 添加到类的数组中。 If my explanation isn't through.如果我的解释没有通过。 runApology('I', 'am', 'sorry', * 2);

HTML 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 .我建议使用Lodash It has many in-built comparison functions.它具有许多内置的比较功能。 You should use the isEqual() for the same.你应该使用 isEqual() 来做同样的事情。 Kindly refer the following code.请参考以下代码。

_.isEqual(obj1,obj2);

For further reference you can refer this documentation .如需进一步参考,您可以参考此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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