0

I need to loop through an entire 2D array (OldTable) to check that Column1 has a value of 1 and Col7 is not empty (null). If the above conditions are true then push the current (i) arrays of elements into newTable. My snippet of JS is as follow...

var newTable = [];  
  for (var i=1; i<OldTable.length; i++){     
    if(OldTable[i][0]==1 && OldTable[i][7]!==null){
      newTable.push(OldTable[i]);      
    }    
  }  

Seems like a fairly straight forward thing to do but currently hitting brick wall with this error...

TypeError: Cannot read property "0" from undefined. (line 80, file "Code"

I have tried to reduce the if statement to just...

if(OldTable[i][0]==1){

...but still the same error. I'm able to display the array element just fine using...

Browser.msgBox(OldTable[50][0]);

I'm fairly new to JS so could be a simple silly error someone could point out.

UPDATE: In trying to simplying naming, I've actually made it more difficult with conflicting terminology, so have going through and updated the variable names used.

11
  • 1
    did you mean newArray instead of Array inside of your loop? Commented Feb 17, 2017 at 5:56
  • What is Array? Check if its of proper type Commented Feb 17, 2017 at 5:58
  • Array ??? like, the built-in Array object that comes with every javascript engine since the 90's? Commented Feb 17, 2017 at 5:59
  • 1
    how is the var 'Array' built, which data in it ? Are you sure that its indexes are (consecutive) numbers and not strings ? PS please not that you stast looping from 1 and not from 0 Commented Feb 17, 2017 at 6:05
  • 1
    @ankith OldTable is defined and has length property as code is entering loop. Issue is in value of OldTable[i]. Its not an array Commented Feb 17, 2017 at 6:21

1 Answer 1

1

Your code should work if, as noted in the comment by @Massimo, you change your loop from starting at i=1 to i=0, as shown below. Also, just to whet your appetite for more modern tools within JavaScript, I also include an essentially identical solution to the problem using ES6/ES2015.

var myArray = [
  [1, 0, 0, 0, 0, 0, 0, 'foo'    ], // should pass
  [9, 1, 1, 1, 1, 1, 1, 'foo'    ], // should fail
  [1, 2, 2, 2, 2, 2, 2, 'foo'    ], // should pass
  [1, 3, 3, 3, 3, 3, 3, null     ], // should fail
  [0, 4, 4, 4, 4, 4, 4, null     ], // should fail
  [1, 5, 5, 5, 5, 5, 5, undefined], // should pass
  [1, 6, 6, 6, 6, 6, 6, 'foo'    ]  // should pass
];

function f1(array) {
  var newArray = [];
  for (var i = 0; i < array.length; i++) {
    if (array[i][0] == 1 && array[i][7] !== null) {
      newArray.push(array[i]);
    }
  }
  return newArray;
}

const f2 = array => array.filter(e => e[0] === 1 && e[7] !== null);

console.log(f1(myArray));
console.log(f2(myArray));

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

5 Comments

Yes changing loop to start from 0 and using .filter are good suggestions but how do they solve the problem?
you are a legend! changing i starting value did work for me, another newbee error. I'll definitely have a play with your sample code to see where they can applied. Thanks once again.
@Rajesh, your comment is totally valid, as my answer didn't really seem to address the error the OP was getting. However, now that OP suggests that "my" answer actually solves the problem, I must emphasize again (as I did in my original answer) that what seems to have been the original problem was originally solved by Massimo (can't link to Rajesh and Massimo in same comment), not me. Anyway, glad I could help in a round-about way, and hopefully the ES2015 version will inspire.
@AndrewWillems Glad you wish to share credits with others. Keep up the good work. I'm still not sure how can changing i to 0 will solve the error, but it did. Good for OP but not sure if this adds anything to portal. Just would like to advice you to wait next time for more clarity. Answering with partial knowledge can attract downvotes. :-)
@user1488934 An advice to you is to use better naming convention. Try to avoid generic terms. Name should signify purpose. If not used properly, you will have to read through code just to make sense of what and why.

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.