Reputation: 1633
I'am struck here that mapping a JSON in a array.
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 :
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));
Below I have mentioned expected JSON in array.
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.
Upvotes: 0
Views: 117
Reputation: 1169
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));
Upvotes: 1