所以我有以下由服務器生成的javascript對象。該對象被稱爲$ scope.activityResults使用Jquery檢查是否存在某個Javascript對象
[{
id: 2010,
updateId: 1,
userId: 2,
points: 10
}, {
id: 2011,
updateId: 2,
userId: 3,
points: 100
}];
然後我有一個新建項目,我想,以確定用戶id是否存在於物體與否。如果用戶標識存在,我想要做X,如果不存在,我想這樣做Y.
var newResult = {
id: 0,
updateId: 3,
userId: 10,
competitionId: "2014354864",
result: $scope.activityPointsValue,
time: new Date()
}
我掙扎的最佳途徑找出該檢查用戶id是否已經存在於對象與否。
會喜歡一些幫助。
if (newResult.userId exists in $scope.activityResults)) { //This line needs the help :)
console.log("You already have some points");
} else {
console.log("Didnt find you, adding new row :)");
}
'x'in newResult – JAYBEkster
jQuery不會幫你在這裏。 – Bergi
真的啊,會怎麼樣? :=) – user2656127