我在将对象存储到数组时遇到问题。但是当我尝试使用 $.inArray 检查对象的存在时,它总是返回-1。我正在使用 angularjs。
<input name="{{question.number}}"
ng-click="radioSelected(question.choiceOneWeight, question.number,question.tmiGroup)"
type="radio" value="{{question.choiceOneWeight}}" />
$scope.radioSelected = function(choice, question, tmiGroup) {
var answer = {};
answer.questionNumber = question;
answer.answerWeight = choice;
answer.tmiGroup = tmiGroup;
if($.inArray(answer,userAnswers) < 0){
userAnswers.push(answer);
}
};
我生成的答案对象是
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"}
如果我多次单击单选按钮,
userAnswers 数组看起来像:
[{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"},
{"questionNumber":23,"answerWeight":1,"tmiGroup":"JP"}]