My task is here as follows :
Input : A complex/nested JSON object with nested arrays as well objects
Output : A JSON object that contains a set of URLs with their titles: ( key,value ) pair eg
{"Title1":"URL1","Title2":"URL2"}
In this code, I simply go through the JSON object battle
. My task is to append all these values and form a URL string. The only catch is that the number of URLs is a cross product of the number of elements in the Random
property of the FightDetails
property. FightDetails
is defined as an array as there can be more than 1 Fight. The number of URLs generated for each fight can be calculated by : Combat.FightDetails[i].Random[0].length X Combat.FightDetails[i].Random[1].length X Combat.FightDetails[i].Random[2].length X..... X Combat.FightDetails[i].AddToRandom.numberOfElements
.
fun()
is a recursive function
PS: The variable naming may be awkward (hence ignore), but that is due to privacy concerns.
I have the following Javascript Code to generate the set of URLs.
It runs pretty well. I just want to know if there are any redundancies or quality improvements.
NOTE: I'm running this on an expressjs server. Please let me know in case someone needs the express app code to be able to run that. I have made the URL() function exportable.
var URL={};
var data = {};
function fun(i, j, dim, raw, fightSpecifics,metfield,URLMetric,URLCollector,Winner)
{
if (i >= dim.length)
{
var num=1;
var u = "www.fightmosnters.com/?"+URLMetric;
for (var k = 0; k < i; k++)
{
metfield=metfield + " " + fightSpecifics[k];
}
for(prop in raw)
{
if(raw.hasOwnProperty(prop))
{
var fin ="";
fin = "fightSpecifics=(" + metfield +" "+ raw[prop]+")";
u=u+"FightDetails"+ num++ +"=" +URLCollector+" "+fin+"&";
}
}
data[metfield]=u;
u=u+"Winner="+Winner+ "&"+"complete=true";
return;
}
if(j >= dim[i].length)
{
return;
}
fightSpecifics[i] = dim[i][j];
fun(i + 1, 0,dim,raw, fightSpecifics,metfield,URLMetric,URLCollector,Winner);
fun(i, j + 1,dim,raw, fightSpecifics,metfield,URLMetric,URLCollector,Winner);
}
module.exports = function URL()
{
var patNum=1;
var battle = {
"Combat":{
"Fighters":[
"Fighter1",
"Fighter2"
],
"FightDetails":[
{
"FightName":"Plassey",
"Era":"Medieval",
"BeginDate":"14th Century",
"End Date":"17th Century",
"Random":[
[
"Random_1_1",
"Random_1_2"
],
[
"Random_2_1",
"Random_2_2"
],
[
"Random_3_1",
"Random_3_2",
"Random_3_3"
]
],
"AddToRandom":[
"AddedRandomness1",
"AddedRandomness2",
"AddedRandomness3"
],
"Winner":"Goku"
}
]
}
};
for(var fightDetail in battle)
{
var fightGeneralDetails="";
console.log(fightDetail);
if(battle.hasOwnProperty(fightDetail))
{
for(var i=0;i<battle[fightDetail].Fighters.length;i++)
{
fightGeneralDetails= battle[fightDetail].Fighters[i]+ " " + fightGeneralDetails;
}
var num=1;
for(var i=0;i<battle[fightDetail].FightDetails.length;i++)
{
var fightSpecifics="";
for(var prop in battle[fightDetail].FightDetails[i])
{
num=1;
if(battle[fightDetail].FightDetails[i].hasOwnProperty(prop))
{
if(!(prop=="Random"||prop=="AddToRandom"))
{
for(var temp in battle[fightDetail].FightDetails[i].AddToRandom)
{
fightSpecifics = fightSpecifics + (prop+num++ +"="+battle[fightDetail].FightDetails[i][prop]) +"&" ;
}
}
if(prop=="Random")
{
fun(0,0,battle[fightDetail].FightDetails[i].Random,battle[fightDetail].FightDetails[i].AddToRandom,[],"",fightSpecifics,fightGeneralDetails,battle[fightDetail].FightDetails[i].Winner);
}
}
}
}
console.log(data);
return data;
}
}
}
EDIT : Adding Express JS code for reference (Can be ignored)
var express = require('express');
var app = express();
app.get("/",function(req,res){
var t = require(<Add Path to The JS File>);
t();
})
app.listen(3001);
module.exports = app;
Random
andAddToRandom
properties. Edited as well. Let me know if it still doesn't make sense. Will edit further \$\endgroup\$*
. \$\endgroup\$Random
property has three sub arrays with 2,4,3 objects in the them respectively, then there will be 2*4*3 =24 URLs \$\endgroup\$