0

I've been doing some research and am not getting anywhere. I have a situation where I need an array to contain a possibly variable number of arrays. Based on the example below, do you have any ideas?

var masterArray = [];

function PrepData(inVal) {
  var amt = inVal.split("|");
  for (var i = 0; i < amt.length; i++) {
    // !!!Trouble area!!!
    masterArray[i].push = amt[i];
  };
}

What I'm doing is feeding 12 months' worth of data into this function, so PrepData runs 12 times each time I activate the calling function. The variable inVal can contain something like "9|9|0" or "9|123|470|1500|13". What I'm looking to do is split the incoming value on the pipe and store the results in amt. Then I want to feed each value from amt into individual arrays within masterArray. The good news is that inVal's length is constant once the first value is in, so if the first of 12 iterations splits into 3 pieces, the other 11 will also. To lay it out, here's what I would expect a typical run to produce given this input:

Oct: "1|2|1300"
Nov: "1|3|1400"
Dec: "2|5|1450"
Jan: "3|6|1900"
Feb: "4|8|2015"
Mar: "4|8|2020"
Apr: "19|38|3200"
May: "30|42|3500"
Jun: "32|50|5000"
Jul: "48|72|6300"
Aug: "50|150|7500"
Sep: "80|173|9000"

Once all twelve run through PrepData, I should have an array that contains this:

masterArray[0] == {1, 1, 2, 3, 4, 4, 19, 30, 32, 48, 50, 80}   // All the numbers from the first section
masterArray[1] == {2, 3, 5, 6, 8, 8, 38, 42, 50, 72, 150, 173}   // All the numbers from the second section
masterArray[2] == {1300, 1400, 1450, 1900, 2015, 2020, 3200, 3500, 5000, 6300, 7500, 9000}   // All the numbers from the third section

If each month contained a string with 5 sections, then masterArray would need to be able to go from [0] to [4], and so on. The trouble area above isn't working, so I'm obviously missing something, but don't know what that might be.

1 Answer 1

1

Here is the updated code

var masterArray = [];
function PrepData(inVal){
  var amt = inVal.split("|");
  for (i in amt) {
    if(typeof masterArray[i] == 'undefined'){
        masterArray[i] = [];
    }
    masterArray[i].push(amt[i]);
  }
 }

There is a need to check first if an array is defined in each index in masterArray. If it's not defined then you need to initialize it to a blank array. Then you can push the splited value and you get the same result

masterArray[0] == {1, 1, 2, 3, 4, 4, 19, 30, 32, 48, 50, 80} 
masterArray[1] == {2, 3, 5, 6, 8, 8, 38, 42, 50, 72, 150, 173} 
masterArray[2] == {1300, 1400, 1450, 1900, 2015, 2020, 3200, 3500, 5000, 6300, 7500, 9000}

Here is a demo in js fiddle

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

1 Comment

Brilliant, thank you! I'll test it out ASAP, but it's good to know I was mostly on the right track.

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.