我试图在纯函数中将值移动到父节点

时间:2019-04-11 21:17:10

标签: javascript json functional-programming

我有一个JSON(由giftpeg生成),并且有些节点对我来说是无用的,例如“格式:“ moodle”(请参见代码):

[ 
   { 
       "type": "MC", 
       "title": null, 
       "stem": 
           { 
               "format":"moodle", 
               "text": "What is the main cause of the seasons on the earth?" 
           }, 
           "hasEmbeddedAnswers": false, 
           "globalFeedback": null, 
           "choices": 
           [ { 
               "isCorrect": false, 
               "weight": null, 
               "text": 
               { 
                   "format": "moodle", 
                   "text": "the earth's elliptical (slightly oval) orbit around the sun" }, "feedback": null 
               }, 
               { 
                   "isCorrect": true, 
                   "weight": null, 
                   "text": 
                       { "format": "moodle",
                        "text": "the tilt of earth's axis with respect to the sun"
                       },
                       "feedback": null 
               }, 
               { 
                   "isCorrect": false, 
                   "weight": null, 
                   "text": 
                   { 
                       "format": "moodle", 
                       "text": "the northern hemisphere has twice the land mass than the southern hemisphere"
                   }, 
                   "feedback": null
               } 
           ]
   } 
]

到目前为止,我可以删除“格式”节点(我想我做对了),但是有一些冗余,例如:

"text":{
    "text": "foo"
}

目标是将其(以非破坏性方式)转换为:

"text": "foo"

这是我的代码(尽量发挥作用):

            formatedQuestions(jsonItem) {
            return jsonItem.map(question => {
                question.choices.map(choice => {
                    delete choice.text.format;
                    choice = Object.assign(choice, choice.text);
                    delete choice.text.text;
                    return choice;
                });
                delete question.stem.format;
                return question;
            });

我可以移动它,但它会更改原始JSON。因此,每次我称之为变化。这是一个问题。

这会将值“ up”上移,但这正是改变原始值的原因

 choice = Object.assign(choice, choice.text);

如果您除了解决方案之外还有其他建议,请使其更加实用,高效。

1 个答案:

答案 0 :(得分:1)

 const formatObj = obj => obj.format === "moodle" ? obj.text : Object.entries(obj).reduce((res, [key, value]) => ({ ...res, [key]: format(value) }), {});

 const formatArray = array => array.map(format);

 const format = it => (
   Array.isArray(it) && formatArray(it) ||
   typeof it === "object" && formatObj(it) ||
   it
 );

那是功能,无论是“高效”还是“可读”,都是另一回事。

我通常会怎么做:

 function format(it) {
   if(Array.isArray(it))
     return it.map(format);

   if(typeof it === "object") {
     if(it.format === "moodle")
       return it.text;

     const result = {};
     for(const [key, value] of Object.entries(it))
        result[key] = format(value);
     return result;
   }

   return it;
}

如果您不需要通用解决方案,以下是针对您的用例的特定解决方案:

 formatedQuestions(jsonItem) {
    return jsonItem.map(question => ({
       ....question,
       stem: question.stem.text,
       choices: question.choices.map(choice => ({
         ...choice,
         text: choice.text.text || choice.text,
       })),
   }));
 }