'use strict';

var config = require('../config');
var helper = require('../helper');
var items = require('../data/items');
var competitions = require('../data/competitions');
var express = require('express');
var router = express.Router();

// Route for item
router.route('/:item/:year?')
    .get((req, res) => {
        // Define requested year
        var requestedYear = null;

        if (config.years.availables.some(year => req.params.year === year)) {
            requestedYear = req.params.year;
        } else {
            requestedYear = config.years.current;
        }

        // Set cache time depending on the requested year
        if (requestedYear === config.years.current) {
            var oneDay = 86400000;
            res.setHeader('Cache-Control', 'public, max-age=' + oneDay);
        } else {
            var oneMonth = 2592000000;
            res.setHeader('Cache-Control', 'public, max-age=' + oneMonth);
        }

        // Define requested item
        var requestedItem = null;

        items.forEach(item => {
            if (req.params.item === item.code) {
                requestedItem = item;
            }
        });

        requestedItem = requestedItem || items[0];

        var data = {
            title: 'Dashboard Football - Quick access to ' + requestedItem.name + ' results for season ' + requestedYear,
            description: 'Quick access to ' + requestedItem.name + ' results, tables, top scorers and top assists for season ' + requestedYear,
            items: items,
            years: config.years.availables,
            requestedItem: requestedItem,
            requestedYear: requestedYear,
            helpers: {
                isActive: function (code, options) {
                    return code === requestedItem.code ? options.fn(this) : options.inverse(this);
                },
                isWinner: function (team, winner, options) {
                    return team === winner ? options.fn(this) : options.inverse(this);
                }
            }
        };

        if (requestedItem === competitions.championsLeague || requestedItem === competitions.europaLeague) {
            res.render('competition', Object.assign(data, {
                tournamentData: helper.readJsonFile(helper.stringFormat(config.paths.tournamentData, requestedItem.code, requestedYear)),
                scorersData: helper.readJsonFile(helper.stringFormat(config.paths.scorersData, requestedItem.code, requestedYear)),
                assistsData: helper.readJsonFile(helper.stringFormat(config.paths.assistsData, requestedItem.code, requestedYear))
            }));
        } else {
            res.render('league', Object.assign(data, {
                resultsData: helper.readJsonFile(helper.stringFormat(config.paths.resultsData, requestedItem.code, requestedYear)),
                tableData: helper.readJsonFile(helper.stringFormat(config.paths.tableData, requestedItem.code, requestedYear)),
                scorersData: helper.readJsonFile(helper.stringFormat(config.paths.scorersData, requestedItem.code, requestedYear)),
                assistsData: helper.readJsonFile(helper.stringFormat(config.paths.assistsData, requestedItem.code, requestedYear))
            }));
        }
    });

module.exports = router;
@@ -331,32 +331,90 @@
     
-// Define requested year
+var requestedItem = items.find(item =%3E item.code === req.params.item) %7C%7C items%5B0%5D;
 %0D%0A  
@@ -1030,285 +1030,8 @@
 %0D%0A%0D%0A
-        // Define requested item%0D%0A        var requestedItem = null;%0D%0A%0D%0A        items.forEach(item =%3E %7B%0D%0A            if (req.params.item === item.code) %7B%0D%0A                requestedItem = item;%0D%0A            %7D%0D%0A        %7D);%0D%0A%0D%0A        requestedItem = requestedItem %7C%7C items%5B0%5D;%0D%0A%0D%0A
     

Use new find function