0

I'm looping through an array of file names, splitting the names, and storing the data in an object. The two file names for testing purposes are identical except for the week "number" which should create two separate weeks. The problem is that the first entry is being overwritten by the last iteration so I end up with an entry for week 2 only.

The code:

const planList = [
    'military_greekHero_achilles_week_1.htm',
    'military_greekHero_achilles_week_2.htm'
];

var _completePlan = {};

planList.forEach(_plan => {

// Pull data from the file name formated: target_series_title_overview/week_weekNum.htm
    let _planPieces = _plan.split('.')[0].split('_'),// Drop the .htm
        _planTarget = _planPieces[0],
        _planSeries = _planPieces[1],
        _planTitle = _planPieces[2],
        _planOverview = _planPieces[3],
        _planWeek = _planPieces[4];

    _planOverview = _planOverview == 'overview' ? true : false;
    
// Start Building Plan Object
    _completePlan[_planTitle] = {
        info: {},
        weeks: {}
    }

// _planWeek logs 1 and 2 while iterating but entry for .weeks.1 is overwritten with .weeks.2
    _completePlan[_planTitle].weeks[_planWeek] = {
        sn: { inactive: true },
        mo: { inactive: true },
        tu: { inactive: true },
        we: { inactive: true },
        th: { inactive: true },
        fr: { inactive: true },
        st: { inactive: true }
    }
});

console.log(_completePlan);
});

I feel like I'm missing something simple... any ideas?

2 Answers 2

1

You just need to check if the object already exists before you try and create it (thus overwriting the previous):

if (!_completePlan.hasOwnProperty(_planTitle)) {
    _completePlan[_planTitle] = {
      info: {},
      weeks: {}
    }
  }

Also I added in a little restructuring statement that helps reduce some code:

let [_planTarget, _planSeries, _planTitle, _planO, _planWeek] = _plan.split('.')[0].split('_'), // Drop the .htm
_planOverview = _planO === 'overview' ? true : false;

const planList = [
  'military_greekHero_achilles_week_1.htm',
  'military_greekHero_achilles_week_2.htm'
];

var _completePlan = {};

planList.forEach(_plan => {

  // Pull data from the file name formated: target_series_title_overview/week_weekNum.htm
  let [_planTarget, _planSeries, _planTitle, _planO, _planWeek] = _plan.split('.')[0].split('_'), // Drop the .htm
    _planOverview = _planO === 'overview' ? true : false;

  // Start Building Plan Object
  if (!_completePlan.hasOwnProperty(_planTitle)) {
    _completePlan[_planTitle] = {
      info: {}, weeks: {}
    }
  }

  _completePlan[_planTitle].weeks[_planWeek] = {
    sn: { inactive: true},
    mo: { inactive: true},
    tu: { inactive: true},
    we: { inactive: true},
    th: { inactive: true},
    fr: { inactive: true},
    st: { inactive: true}
  }
});

console.log(_completePlan);

Sign up to request clarification or add additional context in comments.

Comments

0

You are resetting the whole of _completePlan[_planTitle] with each iteration. So the 1 or 2 objects inside the weeks object are not getting "overridden" per se, but rather their parent's parent object is being reset to {info: {}, weeks: {}}.

So you need to set the weeks object to itself if it exists, and only if it doesn't to a blank object.

Here is how you would do that:

const planList = [
    'military_greekHero_achilles_week_1.htm',
    'military_greekHero_achilles_week_2.htm'
];

var _completePlan = {};


planList.forEach(_plan => {

        // Pull data from the file name formated: target_series_title_overview/week_weekNum.htm
            let _planPieces = _plan.split('.')[0].split('_'),// Drop the .htm
                _planTarget = _planPieces[0],
                _planSeries = _planPieces[1],
                _planTitle = _planPieces[2],
                _planOverview = _planPieces[3],
                _planWeek = _planPieces[4];

            _planOverview = _planOverview == 'overview' ? true : false;
            
        // Start Building Plan Object
            

        // _planWeek logs 1 and 2 while iterating but entry for .weeks.1 is overwritten with .weeks.2
            _completePlan[_planTitle] = _completePlan[_planTitle] || {};
            _completePlan[_planTitle].info = _completePlan[_planTitle].info || {};
            _completePlan[_planTitle].weeks = _completePlan[_planTitle].weeks || {};


            _completePlan[_planTitle].weeks[_planWeek] = {
                sn: { inactive: true },
                mo: { inactive: true },
                tu: { inactive: true },
                we: { inactive: true },
                th: { inactive: true },
                fr: { inactive: true },
                st: { inactive: true }
    }
});
console.log(_completePlan);

2 Comments

Lol holy crap thank you. As soon as I read the first line of your response I face palmed.
Don't worry It happens to the best of us :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.