但我相信我做错了,因为它没有多大帮助。商店仍然太复杂,因此我需要在reducer中使用一些递归的东西来更新状态。
此外,深度嵌套的状态使得使用react-redux connect()非常困难,因为它只进行了浅层比较。
响应的形状如下:
const questionData = {questions:[
{
id: '1312fsadf123',
state: QUESTION.state[0].value,
chapter_id: '',
content_type: '',
question_type: '',
difficult_degree: '',
question: {
content: [],
answers: [
{
id: '324',
text: [],
answer_key: 'a',
correct: true,
},
{
id: '323',
text: [],
answer_key: 'b',
correct: true,
},
{
id: '322',
text: [],
answer_key: 'c',
correct: true,
},
{
id: '321',
text: [],
answer_key: 'd',
correct: true,
},
],
},
solution_suggesstion: [],
solution_detail: [],
note: [],
attachment: { questionFile: '', solutionDetailFile: '' },
question_child: ['1213'],
},
{
id: '1213',
state: QUESTION.state[0].value,
chapter_id: '',
content_type: '',
question_type: '',
difficult_degree: '',
question: {
content: [],
answers: [
{
id: '324',
text: [],
answer_key: 'a',
correct: true,
},
{
id: '323',
text: [],
answer_key: 'b',
correct: true,
},
{
id: '322',
text: [],
answer_key: 'c',
correct: true,
},
{
id: '321',
text: [],
answer_key: 'd',
correct: true,
},
],
},
solution_suggesstion: [],
solution_detail: [],
note: [],
attachment: { questionFile: '', solutionDetailFile: '' },
},
]};
我试过使用这样的normalizr工具:
const answer = new schema.Entity('answers');
const question = new schema.Entity('questions');
const mySchema = { questions: [question] };
export const normalizedData = normalize(questionData, mySchema);
console.log('normalizedData', normalizedData);
myConsole 我希望它是
{
entities: {
questions: {1213: {…,question.answers:[324,323,322,321]}, 1312fsadf123: {…}},
answer: [{...},{...},{...}]
},
result:{question:[1213,1312fsadf123]}}
请帮助我!
答案 0 :(得分:0)
试试这个:
const questionData = {
questions: [{
id: '1312fsadf123',
state: 'QUESTION.state[0].value',
chapter_id: '',
content_type: '',
question_type: '',
difficult_degree: '',
question: {
content: [],
answers: [{
id: '324',
text: [],
answer_key: 'a',
correct: true,
},
{
id: '323',
text: [],
answer_key: 'b',
correct: true,
},
{
id: '322',
text: [],
answer_key: 'c',
correct: true,
},
{
id: '321',
text: [],
answer_key: 'd',
correct: true,
},
],
},
solution_suggesstion: [],
solution_detail: [],
note: [],
attachment: {
questionFile: '',
solutionDetailFile: ''
},
question_child: ['1213'],
},
{
id: '1213',
state: 'QUESTION.state[0].value',
chapter_id: '',
content_type: '',
question_type: '',
difficult_degree: '',
question: {
content: [],
answers: [{
id: '324',
text: [],
answer_key: 'a',
correct: true,
},
{
id: '323',
text: [],
answer_key: 'b',
correct: true,
},
{
id: '322',
text: [],
answer_key: 'c',
correct: true,
},
{
id: '321',
text: [],
answer_key: 'd',
correct: true,
},
],
},
solution_suggesstion: [],
solution_detail: [],
note: [],
attachment: {
questionFile: '',
solutionDetailFile: ''
},
}
]
};
const baseQuestions = questionData.questions;
const { questions, answers } = baseQuestions.reduce(({ questions = {}, answers = [] } = {}, baseQuestion) => {
const { id: questionID, question: { answers: baseAnswers } } = baseQuestion;
const answersInQuestionProperty = baseAnswers.map(({ id }) => Number(id));
const newQuestionObj = Object.assign({ answers: answersInQuestionProperty }, baseQuestion);
questions[questionID] = newQuestionObj;
answers.push(baseAnswers);
return { questions, answers };
}, {});
const finalObj = { entities: { questions, answers }};
finalObj.result = { question: Object.keys(questions) };
console.log(finalObj);

这是一般性的想法 - 只需使用.reduce
并继续提取和解构,直到数据按您希望的方式构建。