js 對象、數(shù)組的定義,及數(shù)組元素查找是否包含子項
發(fā)布日期:2022/10/11 作者:
瀏覽:720
var AllowedGrantTypes = []; //在循環(huán)體外面定義一個全局?jǐn)?shù)組對象,等一下往這里面PUSH進內(nèi)容
.... 省略若干行
onSelect: function(record){
var GrantType={}; //定義一個子項,要push進數(shù)組中
GrantType.name=record.name; //添加字段
GrantType.id=record.id; //添加字段
console.log("+=:"+record.name + record.id);
//if (AllowedGrantTypes.indexOf(GrantType) < 0) { //這樣子查找是沒有結(jié)果的,就結(jié)果存在也會返回-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。
下拉加載更多評論