【问题标题】:How to verify array within object如何验证对象内的数组
【发布时间】:2020-04-16 04:33:39
【问题描述】:

所以我参加了一个训练营,帮助您做好准备进行 Web 开发的工作。我遇到了一个障碍,想​​知道是否有人可以提供一些启示。 inTruth('I', 'is', 'dumb'); 目标是让 Bootstrap 类的方法“registerStudent”,验证它的数组“students”中是否有匹配的对象,如果有,它说“已经有一个学生注册”,如果没有它会添加调用方法并将对象添加到类的数组中。如果我的解释没有通过。 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;
                }
            }

        }

【问题讨论】:

    标签: javascript html arrays class object


    【解决方案1】:

    我建议使用Lodash。它具有许多内置的比较功能。你应该使用 isEqual() 来做同样的事情。请参考以下代码。

    _.isEqual(obj1,obj2);
    

    如需进一步参考,您可以参考此documentation

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 2013-10-22
      • 2021-05-18
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 2015-10-24
      相关资源
      最近更新 更多