{"size": 1428, "ext": "js", "max_stars_repo_path": "app/data-transformation.js", "max_stars_repo_name": "longjasonm/algolia-tedtalks-demo", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/data-transformation.js", "max_issues_repo_name": "longjasonm/algolia-tedtalks-demo", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/data-transformation.js", "max_forks_repo_name": "longjasonm/algolia-tedtalks-demo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 34.8292682927, "max_line_length": 127, "alphanum_fraction": 0.6463585434}
// Data transformation demo
const talks = require('../data/tedtalks/talks.json');
const speakers = require('../data/tedtalks/speakers.json');
const fs = require('fs');
let slimmedDownTalks = [];

function trimTalksDown(talks){
    slimmedDownTalks = talks.map(talk => {
        return {
            objectID: talk.objectID,
            name: talk.name,
            speakers: talk.speakers,
            slug: talk.slug
        }
    });
}
trimTalksDown(talks);


function talksToSpeakers(talks, speakers) {
    // Iterate through all speakers
    for (let i = 0; i < speakers.length; i++) {
        const speaker = speakers[i];
        // filter test: if the name of the speaker equals any of the speakers in the array, then write it to the new attribute.
        const speakerName = speaker.name;
        const result = talks.filter(talk => talk.speakers.find(speaker => speaker === speakerName));
        // create new key on speaker object for talks, then store array of result objects.
        speaker.talks = result;
    }
}

// Refactor
// speakers.forEach(speaker => {
//     const name = speaker.name;
//     const talkJoin = talks.filter(talk => talk.speakers.find(speaker => speaker === name));
//     speakers.talks = talkJoin;
// });

talksToSpeakers(slimmedDownTalks, speakers);
// Write results to json file.
fs.writeFile('../data/speakers_with_talks.json', JSON.stringify(speakers), (err) => {if (err) throw err});