js 對(duì)象、數(shù)組的定義,及數(shù)組元素查找是否包含子項(xiàng)
發(fā)布日期:2022/10/11 作者:
瀏覽:700
var AllowedGrantTypes = []; //在循環(huán)體外面定義一個(gè)全局?jǐn)?shù)組對(duì)象,等一下往這里面PUSH進(jìn)內(nèi)容
.... 省略若干行
onSelect: function(record){
var GrantType={}; //定義一個(gè)子項(xiàng),要push進(jìn)數(shù)組中
GrantType.name=record.name; //添加字段
GrantType.id=record.id; //添加字段
console.log("+=:"+record.name + record.id);
//if (AllowedGrantTypes.indexOf(GrantType) < 0) { //這樣子查找是沒(méi)有結(jié)果的,就結(jié)果存在也會(huì)返回-1
// AllowedGrantTypes.push(GrantType);
//}
if (AllowedGrantTypes.findIndex(item=>item.id==GrantType.id && item.name==GrantType.name) < 0) { //用這種方法去查找比較保險(xiǎn),若不存在,則push
AllowedGrantTypes.push(GrantType);
}
console.log("length=:"+AllowedGrantTypes.length);
},
aa
如上圖所示,AllowedGrantTypes中明明存在granttype項(xiàng),但是返回FALSE。
下拉加載更多評(píng)論