Forum Discussion

william_roe's avatar
william_roe
Super Contributor
9 years ago
Solved

Pushing Items onto Array

How do you push items onto an array? The following throws an exception.

 

function ReadAllCribs() {
    var AllCribs = {};
    
    Project.Variables.Cribs.Reset();
    for (; !Project.Variables.Cribs.IsEOF() ;) {
        AllCribs.push(Project.Variables.Cribs.Value("CribNumber")); //<<< Exception

        Project.Variables.Cribs.Next();
    }
  • Your all cribs array object should be created with square brackets, using braces creates an object not an array object. I'm going to take a guess here and assume you were trying to append the value with a string "CribNumber" before adding it to array.

     

     

    function readAllCribs() {
    
    
        var allCribs = [];
        
        Project.Variables.Cribs.Reset();
        while (!Project.Variables.Cribs.IsEOF() {
            allCribs.push("CribNumber" + Project.Variables.Cribs.Value); 
            Project.Variables.Cribs.Next();
        }
    
    
    
    
    }

     

     

     

    }

5 Replies

  • chrisb's avatar
    chrisb
    Regular Contributor

    Your all cribs array object should be created with square brackets, using braces creates an object not an array object. I'm going to take a guess here and assume you were trying to append the value with a string "CribNumber" before adding it to array.

     

     

    function readAllCribs() {
    
    
        var allCribs = [];
        
        Project.Variables.Cribs.Reset();
        while (!Project.Variables.Cribs.IsEOF() {
            allCribs.push("CribNumber" + Project.Variables.Cribs.Value); 
            Project.Variables.Cribs.Next();
        }
    
    
    
    
    }

     

     

     

    }

    • william_roe's avatar
      william_roe
      Super Contributor

      Thank you. Missed a closing bracket.

       

      // returns list of all cribs
      function ReadAllCribs() {
          var AllCribs = [];
          
          Project.Variables.Cribs.Reset();
          while (!Project.Variables.Cribs.IsEOF()){
              AllCribs.push(Project.Variables.Cribs.Value("CribNumber"));
      
              Project.Variables.Cribs.Next();
          }
          return AllCribs;
      }
  • chrisb's avatar
    chrisb
    Regular Contributor

    Whats the error message and is it caused by the semicolon directly after the for statement? that doesnt look right to me.

    • william_roe's avatar
      william_roe
      Super Contributor

      The error was not cause by the semicolon but the curly bracket on declaration.


      chrisb wrote:

      Whats the error message and is it caused by the semicolon directly after the for statement? that doesnt look right to me.


       

  • chrisb's avatar
    chrisb
    Regular Contributor

    Also, I think you should be using 'while' rather than 'for' as you are running that loop while a condition is not true, ie. not the end of file.