0

Im currently using node.js for a project, if you can, please help. Busy making a to-do list, and want to add listname to numlists, will this work?

rl.question("What would you like to do?", function(firstAnswer){

if(firstAnswer == '1'){

    var addList = [];
    var numLists = [];
    for( i = 0; i< numlists.length; i++){

        rl.question("What is the list's name?", function(listName){
            numLists[i] = listName;
            rl.question("Do you want to add another list?", function
        }



    }

}
    else if(firstAnswer == '2'){




    }
        else if(firstAnswer == '3'){




        }

2 Answers 2

1

There is another and easy way to concatenate arrays in JavaScript:

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = [...hege, ...stale];

Output will be: [ 'Cecilie', 'Lone', 'Emil', 'Tobias', 'Linus' ]

And the advantage of this method that you can add extra items between the two arrays like this:

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = [...hege, 'jon', ...stale, 'jeff'];

And the output will be: [ 'Cecilie', 'Lone', 'jon', 'Emil', 'Tobias', 'Linus', 'jeff' ]

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

Comments

1

From what i understood, below is the answer if you want to combine array.

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);

Children Output : ["Cecilie","Lone","Emil","Tobias","Linus"]

Comments

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.