怎么判断对象组内是否有对象拥有该属性?

小程序 文章 2022-01-30 20:40 389 0 全屏看文

AI助手支持GPT4.0

怎么判断对象组内是否有对象拥有该属性?How to judge whether there is an object in the object group that has the attribute?

怎么判断对象组内是否有对象拥有该属性?

比如这里,我在data定义了一组个对象组 ,有三个对象分别对应账号密码,那我应该怎么在js文件里编写函数,来实现判断用户输入的用户名(假设为小明)是否已经存在在这个对象组里面的?(就是已经存在的对象是否有叫小明的)网上一直没找到这个解决方法……挺难受的

For example, here, I define a set of object groups in data. There are three objects corresponding to account and password respectively. How can I write a function in JS file to judge whether the user name entered by the user (assuming Xiao Ming) already exists in this object group? (i.e. whether the existing object is called Xiaoming) I haven't found this solution online... It's very uncomfortable

回答:

微盟:
const res = student_list.some(item=>item.username==='张三');console.log(res); // true | false
what:谢谢谢谢
brave:
student_list.find(item => item.username == "张三")
what:太感谢了,for循环没这个好用
拾忆:

循环匹配~

what:for循环每个对象,然后判断属性是否匹配吗?
拾忆:循环数组,然后判断对象的属性是否符合,符合跳出循环。
what:太感谢了,就是还有一个地方不明白,怎么跳出循环,Python是break,这里也是吗?如果想终止整个自己定义的函数运行呢?
what:就是在js像onload那么大的函数怎么终止运行
拾忆:Python不清楚,js是return或break。

-EOF-

AI助手支持GPT4.0