首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助比较数组中的值,并使用forEach循环

需要帮助比较数组中的值,并使用forEach循环
EN

Stack Overflow用户
提问于 2015-03-24 21:38:14
回答 1查看 66关注 0票数 0

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

代码语言:javascript
复制
 persons = [object, object, object] 
 where each object has { user:number , username: string, latitude:number, longitude:number}

从这里我试着找出我的用户/用户名是否在这些对象中,如果没有找到要创建的id,如果发现id类似于它来更新它们的位置。听起来很简单,我想这个问题已经不成比例了,但什么也不管用。我现在拥有的代码不起作用,要么我永远找不出用户不在的时候,要么我想不出如何在每次找到一个不是我的用户时停止创建我。

代码语言:javascript
复制
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")
      });
    }
  })
});

任何帮助/想法都是很棒的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-24 22:37:35

当某个异步事件发生时,就会使用这个承诺。由于无法创建这样的事件,所以我编写了如下静态代码:

代码语言:javascript
复制
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());

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29243393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档