2015-03-24 90 views
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") 
     }); 
    } 
    }) 
}); 

任何帮助/想法都会很棒。

+1

,什么是问题? – MaxZoom 2015-03-24 21:41:00

+0

我如何基本解决这个问题,我使用的东西不是forEach循环,没有任何提示?我有我很想重构工作的代码,但我会接受任何想法,以更好地解决实际工作的问题。 – 2015-03-24 21:43:07

+0

因此,您需要向现有的用户集合添加新用户,如果用户不在该集合中? – MaxZoom 2015-03-24 22:04:41

回答

0

Promise用于某些异步事件发生时。 既然不能创造这样的情况下,我做了如下一种静态代码:

var persons = new Array(); 
 

 
persons.indexOf = function(person) { 
 
    var index = -1; 
 
    this.forEach(function(obj, i) { 
 
    if (person.username == obj.username) { 
 
     index = i; 
 
    } 
 
    }); 
 
    return index; 
 
} 
 
     
 
persons.addOrUpdate = function(person) { 
 
    var index = this.indexOf(person); 
 
    if (index == -1) { 
 
    person.user = this.length + 1; 
 
    this.push(person); 
 
    } 
 
    else { // update case 
 
    var obj = this[index]; 
 
    obj.latitude = person.latitude; 
 
    obj.longitude = person.longitude; 
 
    } 
 
} 
 
     
 
persons.print = function() { 
 
    var str = ""; 
 
    this.forEach(function(obj) { 
 
    str += obj.user + ". " + obj.username + " at location (" + 
 
     obj.latitude + ":" + obj.longitude + ")\n"; 
 
    }); 
 
    return str; 
 
} 
 
     
 
persons.addOrUpdate({username:'Adam', latitude:-0.0045, longitude:14.2015}); 
 
persons.addOrUpdate({username:'Eve', latitude:-0.0045, longitude:14.2015}); 
 
persons.addOrUpdate({username:'Abel', latitude:-0.0045, longitude:14.2015}); 
 
// Updating Able location 
 
var person = {username:'Abel', latitude:10.1145, longitude:14.1234}; 
 
persons.addOrUpdate(person); 
 

 
alert(persons.print());