要求
完善編輯器中的every函數(shù),如果集合(collection)中的所有對象都存在對應(yīng)的屬性(pre)衷模,并且屬性(pre)對應(yīng)的值為真。函數(shù)返回ture蒲赂。反之阱冶,返回false。
記桌淖臁:只能通過中括號來訪問對象的變量屬性[pre]木蹬。
分析
1.Array.prototype.every(function())可以滿足數(shù)組循環(huán)需求。
2.arr[pre]訪問對象對應(yīng)的屬性值若皱。
解決
測試
every([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")應(yīng)該返回 true镊叁。
every([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")應(yīng)該返回 false。
every([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age")應(yīng)該返回 false走触。