js 对象、数组的定义,及数组元素查找是否包含子项
发布日期:2022/10/11 作者:
浏览:661
var AllowedGrantTypes = []; //在循环体外面定义一个全局数组对象,等一下往这里面PUSH进内容
.... 省略若干行 onSelect: function(record){ var GrantType={}; //定义一个子项,要push进数组中 GrantType.name=record.name; //添加字段 GrantType.id=record.id; //添加字段 console.log("+=:"+record.name + record.id); //if (AllowedGrantTypes.indexOf(GrantType) < 0) { //这样子查找是没有结果的,就结果存在也会返回-1 // AllowedGrantTypes.push(GrantType); //} if (AllowedGrantTypes.findIndex(item=>item.id==GrantType.id && item.name==GrantType.name) < 0) { //用这种方法去查找比较保险,若不存在,则push AllowedGrantTypes.push(GrantType); } console.log("length=:"+AllowedGrantTypes.length); },
aa
如上图所示,AllowedGrantTypes中明明存在granttype项,但是返回FALSE。
下拉加载更多评论