简体   繁体   English

如何动态地将对象添加到对象的属性?

[英]How do I dynamically add objects to an object's property?

I am building a questionnaire and the questions can have multiple answers and I need to build an array of the question and answers such as: 我正在构建调查表,问题可以有多个答案,我需要构建一系列问题和答案,例如:

{
    question: 'question', 
    answer: {id: 1, answers: 'answer 1'}, 
            {id: 2, answer: 'answer 2'}
}

I need to show a summary of the questions and the answers that the user chose. 我需要显示用户选择的问题和答案的摘要。

For example: Question: Which of these states have you lived in? 例如:问题:您住过以下哪个州? answers: Alabama, Alaska, New Jersey, Rhode Island. 答案:阿拉巴马州,阿拉斯加,新泽西州,罗德岛州。

The resulting object could be: 结果对象可能是:

{
  question: 'Which of these states have you lived in?', 
  answer: {id: 1, answer: 'Alaska'}, 
          {id: 3, answer: 'Rhode Island'}
}

How do I go about dynamically adding the answers while only having the question show up once? 如何仅在问题只出现一次的情况下动态添加答案?

I tried: 我试过了:

var questionAnswerObject = [];
angular.forEach(answersObject, function(value, key){
    questionAnswerObject.push({
        question: question.question, 
        answer: value.answer
    });
});

But of course it shows the question twice. 但是,它当然两次显示了这个问题。

Your missing something very important: you need answers to be an array (or an object), containing all the answers. 您缺少的一些重要信息:您需要answers成为包含所有答案的数组(或对象)。 You can't assign multiple objects to a value the way you are trying to do - they need to be wrapped somehow. 您不能以尝试的方式将多个对象分配给一个值-它们需要以某种方式包装。

So, for example, your answer object might look like this instead: 因此,例如,您的答案对象可能看起来像这样:

var answerObject = {
  question: 'My Question',
  answers: [
    {id: 1, answer: 'first answer'},
    {id: 2, answer: 'second answer'}
  ]
}

Or, you could just make answers a map of the question ids like this: 或者,您可以像这样通过问题ID的映射来作为answers

var answerObject = {
  question: 'My Question',
  answers: {
    1: 'first answer',
    2: 'second answer'
  }
}

To add an answer in the first example, simply do: 要在第一个示例中添加答案,只需执行以下操作:

answerObject.answers.push( {id: 3, answer: 'new answer'} );

To add an answer in the second example, simply do: 要在第二个示例中添加答案,只需执行以下操作:

answerObject.answers[3] = 'new answer';

What I think of you mean your final object to be like this question and array of answers 我对您的想法是您的最终目标,就像这个问题和一系列答案

var arrayObjQuestions = [{
    question: 'question', 
    answer: [{id: 1, answers: 'answer 1'}, 
             {id: 2, answer: 'answer 2'}]
},...];

so if loop throw questions you have and add answer as follow 因此,如果循环抛出问题,并添加答案,如下所示

arrayObjQuestions[0].answer.push({id:1, answer: "answer 1"});
arrayObjQuestions[0].answer.push({id:2, answer: "answer 2"});
arrayObjQuestions[0].answer.push({id:3, answer: "answer 3"});

The best way to achieve this, use: 实现此目的的最佳方法是:

    var questionAnswerObject = {};
    var answers = [];

    answers.push({id:1, answer: 'Alaska'});
    answers.push({id:3, answer: 'Rhode Island'});  

    questionAnswerObject["question"] = 'Which of these states have you lived in?';
    questionAnswerObject["answer"] = answers ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM