Skip to content

Commit

Permalink
for loops to map/reduce/filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreylancaster committed Dec 24, 2018
1 parent 23bef49 commit fc258e9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 114 deletions.
104 changes: 47 additions & 57 deletions duration-per-location/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
"x":20,
"y":90
},
"barHeight": 20,
"useSubLocation": false
"barHeight": 20
}


Expand All @@ -74,62 +73,53 @@
.attr("height", config.height)
/* END CONFIG SETUP */

var totalTime = 0;
var seasons = [];
var allLocations = [];
var uniqueLocations = [];
var locationsArray = [];
var location;

for (var i in episodesData) {
// act on scenes
var scenes = episodesData[i].scenes;
for (var j in scenes){
// calculate length of scene in seconds
var sceneLength = Math.abs(sec(scenes[j].sceneEnd) - sec(scenes[j].sceneStart));
scenes[j].length = sceneLength;

// add location of scene to allLocations array
location = (scenes[j].subLocation && config.useSubLocation) ? scenes[j].location + " - " + scenes[j].subLocation : scenes[j].location;
allLocations.push(location);
}
}

// deduplicate the list of locations from scenes
allLocations = allLocations.filter(onlyUnique).sort();

// modify uniqueLocations
var uniqueLocationsObject = {};
for(i=0; i<allLocations.length; i++){
uniqueLocationsObject[allLocations[i]] = {"name":allLocations[i], "total":0, "1":0, "2":0, "3":0, "4":0, "5":0, "6":0, "7":0, "8":0}
}

// sum scene lengths for locations of scenes
for(var i in episodesData){
if(episodesData[i].scenes){
for(var j in episodesData[i].scenes){
location = (episodesData[i].scenes[j].subLocation && config.useSubLocation) ? episodesData[i].scenes[j].location + " - " + episodesData[i].scenes[j].subLocation : episodesData[i].scenes[j].location;
uniqueLocationsObject[location].total += episodesData[i].scenes[j].length;
uniqueLocationsObject[location][episodesData[i].seasonNum] += episodesData[i].scenes[j].length;

}
}
if(episodesData[i].scenes.length > 0){
seasons.push(episodesData[i].seasonNum);
}
}
console.log(uniqueLocationsObject);

for(var i in uniqueLocationsObject){
locationsArray.push(uniqueLocationsObject[i]);
}

seasons = seasons.filter(onlyUnique).sort();
// console.log(seasons);

// put all scenes in one array
var scenes = e.episodes.reduce(function(acc, val, ind){
var scene = val.scenes.reduce(function(a, v, i){
var sceneLength = Math.abs(sec(v.sceneEnd) - sec(v.sceneStart));
var s = Object.assign({}, v, {
"seasonNum": val.seasonNum,
"episodeNum": val.episodeNum,
"sceneLength": sceneLength
});
return a.concat(s);
}, []);
return [...acc, ...scene];
}, []);
console.log(scenes);

// put all seasons in an array
var seasons = scenes.reduce(function(acc, val, ind){
return [...acc, val.seasonNum];
}, [])
.filter(onlyUnique);

// then put that array into an object with 0 count
var seasonsObj = seasons.reduce(function(acc, val, ind){
var obj = {};
obj[val] = 0;
return Object.assign(acc, obj);
}, {});

// put all locations in one array, make object to count location time
var locations = scenes.reduce(function(acc, val, ind){
return acc.concat(val.location);
}, [])
.filter(onlyUnique)
.map(function(cur, ind){
return Object.assign({}, {"name": cur}, seasonsObj);
});

// go through scenes and add sceneLength values to locations' counts
scenes.forEach(function(val, ind){
var index = locations.findIndex(function(element){
return element.name == val.location;
});
locations[index][val.seasonNum] += val.sceneLength;
})

// build the visualization
var data = locationsArray;
var data = locations;
var barHeight = config.barHeight;

var svg = d3.select("svg"),
Expand All @@ -150,7 +140,7 @@
var z = d3.scaleOrdinal() // or d3.schemeCategory20c between () and no .range
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00", "#FEC574"]);

var keys = d3.keys(data[0]).slice(0,8);
var keys = d3.keys(data[0]).slice(0,Math.max(...seasons));

for (i=0; i<data.length; i++){
var t=0;
Expand Down
106 changes: 49 additions & 57 deletions duration-per-sublocation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"x":20,
"y":90
},
"barHeight": 20,
"useSubLocation": true
"barHeight": 20
}


Expand All @@ -73,62 +72,55 @@
.attr("height", config.height)
/* END CONFIG SETUP */

var totalTime = 0;
var seasons = [];
var allLocations = [];
var uniqueLocations = [];
var locationsArray = [];
var location;

for (var i in episodesData) {
// act on scenes
var scenes = episodesData[i].scenes;
for (var j in scenes){
// calculate length of scene in seconds
var sceneLength = Math.abs(sec(scenes[j].sceneEnd) - sec(scenes[j].sceneStart));
scenes[j].length = sceneLength;

// add location of scene to allLocations array
location = (scenes[j].subLocation && config.useSubLocation) ? scenes[j].location + " - " + scenes[j].subLocation : scenes[j].location;
allLocations.push(location);
}
}

// deduplicate the list of locations from scenes
allLocations = allLocations.filter(onlyUnique).sort();

// modify uniqueLocations
var uniqueLocationsObject = {};
for(i=0; i<allLocations.length; i++){
uniqueLocationsObject[allLocations[i]] = {"name":allLocations[i], "total":0, "1":0, "2":0, "3":0, "4":0, "5":0, "6":0, "7":0, "8":0}
}

// sum scene lengths for locations of scenes
for(var i in episodesData){
if(episodesData[i].scenes){
for(var j in episodesData[i].scenes){
location = (episodesData[i].scenes[j].subLocation && config.useSubLocation) ? episodesData[i].scenes[j].location + " - " + episodesData[i].scenes[j].subLocation : episodesData[i].scenes[j].location;
uniqueLocationsObject[location].total += episodesData[i].scenes[j].length;
uniqueLocationsObject[location][episodesData[i].seasonNum] += episodesData[i].scenes[j].length;

}
}
if(episodesData[i].scenes.length > 0){
seasons.push(episodesData[i].seasonNum);
}
}
console.log(uniqueLocationsObject);

for(var i in uniqueLocationsObject){
locationsArray.push(uniqueLocationsObject[i]);
}

seasons = seasons.filter(onlyUnique).sort();
console.log(seasons);

// put all scenes in one array
var scenes = e.episodes.reduce(function(acc, val, ind){
var scene = val.scenes.reduce(function(a, v, i){
var sceneLength = Math.abs(sec(v.sceneEnd) - sec(v.sceneStart));
var combined = (v.subLocation) ? v.location + " - " + v.subLocation : v.location;
var s = Object.assign({}, v, {
"seasonNum": val.seasonNum,
"episodeNum": val.episodeNum,
"combined": combined,
"sceneLength": sceneLength
});
return a.concat(s);
}, []);
return [...acc, ...scene];
}, []);
console.log(scenes);

// put all seasons in an array
var seasons = scenes.reduce(function(acc, val, ind){
return [...acc, val.seasonNum];
}, [])
.filter(onlyUnique);

// then put that array into an object with 0 count
var seasonsObj = seasons.reduce(function(acc, val, ind){
var obj = {};
obj[val] = 0;
return Object.assign(acc, obj);
}, {});

// put all locations in one array, make object to count location time
var locations = scenes.reduce(function(acc, val, ind){
return acc.concat(val.combined);
}, [])
.filter(onlyUnique)
.map(function(cur, ind){
return Object.assign({}, {"name": cur}, seasonsObj);
});

// go through scenes and add sceneLength values to locations' counts
scenes.forEach(function(val, ind){
var index = locations.findIndex(function(element){
return element.name == val.combined;
});
locations[index][val.seasonNum] += val.sceneLength;
})

// build the visualization
var data = locationsArray;
var data = locations;
var barHeight = config.barHeight;

var svg = d3.select("svg"),
Expand All @@ -151,7 +143,7 @@
var z = d3.scaleOrdinal() // or d3.schemeCategory20c between () and no .range
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00", "#FEC574"]);

var keys = d3.keys(data[0]).slice(0,8);
var keys = d3.keys(data[0]).slice(0, Math.max(...seasons));

for (i=0; i<data.length; i++){
var t=0;
Expand Down

0 comments on commit fc258e9

Please sign in to comment.