需要帮助将数组内的值与 forEach 循环进行比较

Need help comparing the value inside array, with forEach loop

所以基本上我有一些承诺,forEach,我需要解决这个单一问题的很多问题。所以我使用的变量具有以下结构:

 persons = [object, object, object] 
 where each object has { user:number , username: string, latitude:number, longitude:number}

从那里我试图弄清楚我的 user/username 是否在这些对象之一的内部,如果没有找到 id 喜欢它被创建,如果找到找到 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")
      });
    }
  })
});

任何 help/ideas 都会很棒。

当某些异步事件发生时使用 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());