使用JavaScript迭代并映射数组中的JSON [英] Iterate and map JSON in array using javascript

查看:89
本文介绍了使用JavaScript迭代并映射数组中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我很惊讶将JSON映射到数组中.

I'am struck here that mapping a JSON in a array.

在这里我提到了JSON数组.

Here I have mentioned JSON array.

[ { "_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 :

尝试使用javascript获得预期的结果.我在这里得到了一些输出,但是,它与预期结果不匹配.

Trying to get expected result using 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));

下面我提到了数组中预期的JSON.

Below I have mentioned expected JSON in array.

预期结果:

   [
      {
      "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.

推荐答案

您应该遍历JSON数组,并在遇到App_Model_Playlist_Content_Heading或currentObj为null时创建一个新条目,因此它没有标题,像这样

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

    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));

这篇关于使用JavaScript迭代并映射数组中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆