如何从数组中获取ID的文本?反应

时间:2019-01-16 11:04:40

标签: javascript

CurrentUserAnswerVariants:

    {id: "4468cdc8-220d-4634-9d68-6c9920e0cb48", text: "Question 1", 
     minAnswersQuantity: 1, maxAnswersQuantity: 1, canComment: false,
     canComment: false, currentUserAnswerVariants: ["ecc0b93c-8e3b-4661-8f2e-f5382a74d79b"], id: "4468cdc8-220d-4634-9d68-6c9920e0cb48" }
const answers = [];

        this.setState({questionGroups}, () => {

            this.state.questionGroups.map((questionGroup) => {
                questionGroup.questions.map((question) => {
                    // questionGroup.questions.variants.map((variant) => {
                    const currentUserAnswerVariantsVariantIds = question.currentUserAnswerVariants.filter(variant => ["canComment"].indexOf(variant) === -1);
                    const currentUserAnswerVariantsVariantText = question.currentUserAnswerVariants.filter(variant => currentUserAnswerVariantsVariantIds === variant.id).text;
                    const answer = {
                        questionId: question.id
                    };

                    if (!isNull(question.currentUserAnswerComment)) {
                        answer["comment"] = question.currentUserAnswerComment;
                    }

                    if (currentUserAnswerVariantsVariantIds.length) {
                        answer["variantIds"] = currentUserAnswerVariantsVariantIds;
                    }
                    if (currentUserAnswerVariantsVariantText) {
                        answer["variantText"] = currentUserAnswerVariantsVariantText;
                    }

                    answers.push(answer);


                    console.log(questionGroup.questions.variants);
                    // });

                });
            });

        });
    };

如何通过id从variants数组中获取currentUserAnswerVariants值文本?
My array请问您能看到这张图片吗,我的数组在哪里,当我检查某个变体时,我有了新的数组Answer array

2 个答案:

答案 0 :(得分:0)

function idEquals(idToCompare, question) {
    return question.id.indexOf(idToCompare) === 0;
}

currentUserAnswerVariantsVariantIds = question.filter(idEquals.bind(this, currentUserAnswerVariants));

这有效吗? 遵循这些原则,您可以相应地编辑以下代码。

答案 1 :(得分:0)

  1. $ var = $('#id')。val();
  2. $ var ['text']; ->仅在需要单个ID文本的情况下

->或在id上使用foreach并将每个结果推入一个空白数组并打印数组