0
所以基本上我有一些承诺,forEach,只是我需要解决这个单一问题的很多问题。所以我一起工作的变量具有如下结构:需要帮助比较阵列中的值,forEach循环
persons = [object, object, object]
where each object has { user:number , username: string, latitude:number, longitude:number}
从那里我试图找出如果我的用户/用户名是其中的一个对象的内部,如果没有找到ID喜欢创建它,如果发现发现编号喜欢它来更新他们的位置。听起来很简单,我认为这个问题已经不成比例,但没有任何效果。我现在所拥有的代码无法使用,无论我何时无法知道用户何时不在,或者我无法知道如何在每次找到不是我的用户时停止创建我。
var getswamp = function(item, index) {
return new Promise(function(resolve, reject){
var result = false;
if (item.user === user && item.username === username) {
if ((item.latitude !== latitudenew) || (item.longitude !== longitudenew)) {
var id = item.id;
swampdragon.update('locationcurrent', {
user: user,
latitude: latitudenew,
longititude: longitudenew,
username: username,
id: id
}, function (context, data) {
console.log("data updated", data);
result = true;
resolve(result);
}, function (context, data) {
console.log("You may not be updated");
});
} else {
console.log("No location change");
result = true;
}
}else{
if (item.index === person.index){
console.log(person);
resolve(result)
}
}
});
};
person.forEach(function (item, index) {
var swamping = getswamp(item, index);
swamping.then(function (result) {
console.log(result);
if (result === true) {
console.log("We have you");
} else if (result === false && (index === person.length - 1)) {
console.log('Index: ' + index + ' Length of list is ' + person.length);
swampdragon.create('locationcurrent', {
user: user,
latitude: latitudenew,
longititude: longitudenew,
username: username
}, function (context, data) {
console.log("data created", data);
}, function (context, data) {
console.log("You may not be created")
});
}
})
});
任何帮助/想法都会很棒。
,什么是问题? – MaxZoom 2015-03-24 21:41:00
我如何基本解决这个问题,我使用的东西不是forEach循环,没有任何提示?我有我很想重构工作的代码,但我会接受任何想法,以更好地解决实际工作的问题。 – 2015-03-24 21:43:07
因此,您需要向现有的用户集合添加新用户,如果用户不在该集合中? – MaxZoom 2015-03-24 22:04:41