프런트 엔드에서 별을 클릭 할 때마다 question_id
및 rating
두 개의 키가있는 배열에 개체를 삽입합니다. 사용자가 스타의 등급을 변경 한 다음 기존 키의 값을 업데이트하는 경우 (존재하는 경우), 그렇지 않으면 항목을 배열로 푸시합니다.javascript/angularjs에서 해시 키의 기존 값을 업데이트하십시오.
$scope.yellowPages = [] // is defined
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
}
}
내 궁극적 인 목표 아래
코드 샘플은 고유 question_id's
거기 rating
, 배열해야 5 요소를 가지고있다.
감사합니다.
그래서 뭐가 문제입니까? – gforce301
내 질문은 하나의 특정 question_id에 해당하는 등급 하나만 유지하는 방법입니다. 현재 일어나고있는 것은 현재 값을 업데이트하는 것뿐 아니라 새로운 값을 삽입하는 것입니다. 새로운 값을 삽입하고 싶지는 않습니다. 존재하는 경우 특정 키에 해당하는 값을 업데이트하거나 배열에 삽입하십시오. –