Ask a Question

Pushing Items onto Array

SOLVED
william_roe
Super Contributor

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();
    }
5 REPLIES 5
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.

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.

 

 

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();
    }




}

 

 

 

}

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;
}

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.


 

cancel
Showing results for 
Search instead for 
Did you mean: