简体   繁体   English

使用JavaScript迭代并映射数组中的JSON

[英]Iterate and map JSON in array using javascript

I'am struck here that mapping a JSON in a array. 我在这里感到惊讶的是,将JSON映射到数组中。

Here I have mentioned JSON array. 在这里,我提到了JSON数组。

[ { "_id":{ "$oid":"51d84a1e07121c7f442a6e76" }, "_type":[ "App_Model_Playlist_Content_Heading", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"Intro Activities" }, { "_id":{ "$oid":"51e073fd07121c9e4c15a843" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51e0732a07121cb24cbd36b4" } } }, { "_id":{ "$oid":"51d729ab07121c8b4014bd88" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51d7286a07121c443fc4f126" } } }, { "_id":{ "$oid":"51d84abd07121c4844cb9ab3" }, "_type":[ "App_Model_Playlist_Content_Heading", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"Book Discussion Groups" }, { "_id":{ "$oid":"51d84ade07121c0045ebe8c3" }, "_type":[ "App_Model_Playlist_Content_Narrative", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"Book discussion groups take a bit of front loading of information and definitely take practice. After each book group discussion it is necessary to come together as a class to discuss what worked well and what didn't work well. This allows students to reflect on what to do next time to make the group discussions more engaging and successful. There is also an assessment portion. If you would like students to reflect on each other's performance during group discussions. I like to use student recognitions for students to recognize other students for interesting comments or questions during discussion time." }, { "_id":{ "$oid":"51c36cbe07121c9b4f1af6d0" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51c36aa007121c524dc2a6d9" } } }, { "_id":{ "$oid":"51d84a3307121cee43dba549" }, "_type":[ "App_Model_Playlist_Content_Heading", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"Literary Analysis" }, { "_id":{ "$oid":"51d84a3f07121cee43ab551c" }, "_type":[ "App_Model_Playlist_Content_Narrative", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"The literary analysis is used as a formative assessment. Students use the notes from the reading and their discussion groups to analyze an aspect from the story. Student choice is imperative in this part to inspire students to delve deeply into the story and critically think about the symbolism and themes explored throughout. " }, { "_id":{ "$oid":"51d724a907121c633c32d818" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51d7244507121cf33b5700e6" } } }, { "_id":{ "$oid":"51d723e007121cf33b304174" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51d7238e07121cff3bf36405" } } } ]

Javascript : Javascript:

Trying to get expected result using javascript. 尝试使用javascript获得预期的结果。 I got some output here but, its doesn't match with expected result. 我在这里得到了一些输出,但是它与预期结果不匹配。

var aa = JSON;
var array = [];

for(var i=0; i < aa.length; i++){

   var t = {};                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
   t.header = '';
   t.narrative = '';
   t.resource_id = '';

    if(aa[i]._type[0]=='App_Model_Playlist_Content_Heading'){
       t.header = aa[i].text;
    }

    if(aa[i]._type[0]=='App_Model_Playlist_Content_Narrative'){
               t.narrative = aa[i].text;

    }

    if(aa[i]._type[0]=='App_Model_Playlist_Content_Resource'){
            if(typeof aa[i].resource != 'undefined' && aa[i].resource != null && aa[i].resource !=''){
                t.resource_id = aa[i].resource.$id.$oid;
}

}
array.push(t);
}

console.log(JSON.stringify(array));

Below I have mentioned expected JSON in array. 下面我提到了数组中预期的JSON。

Expected Result: 预期结果:

   [
      {
      "header": "Intro Activities",
      "narrative": "",
      "resource": "51e0732a07121cb24cbd36b4,51d7286a07121c443fc4f126"
      },
      {
      "header": "Book Discussion Groups",
      "narrative": "Book discussion groups take a bit of front loading of information and definitely take practice. After each book group discussion it is necessary to come together as a class to discuss what worked well and what didn't work well. This allows students to reflect on what to do next time to make the group discussions more engaging and successful. There is also an assessment portion. If you would like students to reflect on each other's performance during group discussions. I like to use student recognitions for students to recognize other students for interesting comments or questions during discussion time.",
      "resource": "51c36aa007121c524dc2a6d9"
      },    
      {
      "header": "Literary Analysis",
      "narrative": "The literary analysis is used as a formative assessment. Students use the notes from the reading and their discussion groups to analyze an aspect from the story. Student choice is imperative in this part to inspire students to delve deeply into the story and critically think about the symbolism and themes explored throughout.",
      "resource": "51d7244507121cf33b5700e6,51d7238e07121cff3bf36405"
      }
]

Help me to get the expected result from the above coding. 帮助我从上面的编码中获得预期的结果。 Thanks in advance. 提前致谢。

You should iterate over JSON array, and create a new entry either if you faces an App_Model_Playlist_Content_Heading or the currentObj is null, thus, it has no heading, like this 您应该遍历JSON数组,并在面对App_Model_Playlist_Content_Heading或currentObj为null时创建一个新条目,因此它没有标题,例如

    var array = [],
    currentObj,
    pushNew = function(header, narrative, resourceId) {
        var newObj = {
           header: header || '',
           narrative: narrative || '',
           resource_id: resourceId? [resourceId] : []
        };

        array.push(newObj);

        return newObj;
    }
;

for(var i=0; i < aa.length; i++){
    // Starting of a new entry
    if(aa[i]._type[0]=='App_Model_Playlist_Content_Heading'){
       currentObj = pushNew(aa[i].text);
    }

   if(aa[i]._type[0]=='App_Model_Playlist_Content_Narrative'){

       if(!currentObj) {
          currentObj = pushNew(null, aa[i].text);
       } else {
          currentObj.narrative = aa[i].text;
       }
    }

   if(aa[i]._type[0]=='App_Model_Playlist_Content_Resource'){
      if(typeof aa[i].resource !== 'undefined' && aa[i].resource != null && aa[i].resource !==''){

              if(!currentObj) {
                 currentObj = pushNew(null, null, aa[i].resource.$id.$oid);
              } else {
                 currentObj.resource_id.push(aa[i].resource.$id.$oid);
              }
          }

   }
}

console.log(JSON.stringify(array));

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

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