Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更新javascript/angularjs中散列中键的现有值_Javascript_Angularjs_Arrays_Rating System - Fatal编程技术网

更新javascript/angularjs中散列中键的现有值

更新javascript/angularjs中散列中键的现有值,javascript,angularjs,arrays,rating-system,Javascript,Angularjs,Arrays,Rating System,每当用户单击前端上的星号时,我就用两个键将一个对象插入数组中question\u id和rating。我要寻找的是,如果用户更改任何星的评级,然后更新现有键(如果存在)的值,否则将条目推送到数组中 下面是代码示例 $scope.yellowPages=[]//已定义 if ($scope.yellowPages.length > 1) { for (var i = 0 ; i < $scope.yellowPages.length; i++) { if ($sco

每当用户单击前端上的星号时,我就用两个键将一个对象插入数组中
question\u id
rating
。我要寻找的是,如果用户更改任何星的评级,然后更新现有键(如果存在)的值,否则将条目推送到数组中

下面是代码示例

$scope.yellowPages=[]//已定义

if ($scope.yellowPages.length > 1) {
    for (var i = 0 ; i < $scope.yellowPages.length; i++) {
      if ($scope.yellowPages[i]["question_id"] == question_id) {
          $scope.yellowPages[i]["rating"] = rating; // here updating the existing value of key, but what is happening it's updates and as well as creates a new entry with the updated value.
      }
      else{
        $scope.yellowPages.push({rating: rating, question_id: question_id});
      } // if not present
    }
  }
  else{
    $scope.yellowPages.push({rating: rating, question_id: question_id}); // for 1st time
  } 
}
if($scope.yellowPages.length>1){
对于(变量i=0;i<$scope.yellowPages.length;i++){
如果($scope.yellowPages[i][“问题id”]==问题id){
$scope.yellowPages[i][“rating”]=rating;//此处更新键的现有值,但正在发生的情况是,它会更新并使用更新的值创建一个新条目。
}
否则{
$scope.yellowPages.push({rating:rating,question\u id:question\u id});
}//如果不在场
}
}
否则{
$scope.yellowPages.push({rating:rating,question_id:question_id});//第一次
} 
}
我的最终目标是要有一个唯一的
问题id
,在那里
评级
,数组应该只有5个元素


谢谢

这与for循环中的if/else有关。您正在检查forloop迭代中的当前元素是否是相同的问题:

if ($scope.yellowPages[i]["question_id"] == question_id) {
如果不是,则将项目推入数组:

$scope.yellowPages.push({rating: rating, question_id: question_id});

循环的每次迭代都会发生这种情况。因此,例如,如果数组中有3个项目,并且匹配的问题id是第三个项目,那么在到达第三个索引中的匹配对象并更新其评级之前,您将把一个新对象($scope.yellowPages.push({rating:rating,question_id:question_id});)推入数组两次。

为什么要使用数组?而是使用如下所示的对象:

$scope.yellowPages = {
   "question_id1": "rating1",
   "question_id2": "rating2"
}

有了它,您可以轻松访问您的问题,而无需循环。

我这样做只是为了更新现有值,因为我只需要5个值出现在数组中,所以我只在插入5个初始值后进行检查。然而,我认为这不是实现它的最佳方式,但这解决了我的问题

if ($scope.yellowPages.length >= 5) {
    for (var i = 0 ; i < $scope.yellowPages.length; i++) {
      if ($scope.yellowPages[i]["question_id"] == question_id) {
        $scope.yellowPages[i]["rating"] = rating;
      }
    }
  }
  else{
    $scope.yellowPages.push({rating: rating, question_id: question_id});
  }
如果($scope.yellowPages.length>=5){
对于(变量i=0;i<$scope.yellowPages.length;i++){
如果($scope.yellowPages[i][“问题id”]==问题id){
$scope.yellowPages[i][“评级”]=评级;
}
}
}
否则{
$scope.yellowPages.push({rating:rating,question\u id:question\u id});
}

那么问题是什么呢?我的问题是如何只保持一个评级对应于一个特定的问题id,现在发生的是它正在更新当前值以及插入新值。我不想让它插入新的值,只要更新与该特定键对应的值(如果存在)或将其插入数组中即可。我想你是对的,Ken,但我该如何修复它,有什么建议吗?@VibhooMishra在foreach循环中不使用else语句,可以在foreach循环之前使用设置为false的布尔值。然后在foreach循环中,如果发现问题,则将布尔值设置为true。然后在foreach循环之后,检查是否找到了问题,如果没有,则将问题推送到数组中。下面是一些更新的代码示例,Edit:updatewithpastebin