0

I have a JS function with for loops. Inside the nested for loops, str element prints all of the intended elements. But, outside it doesn't print all of it. I would appreciate any help. Here is my code:

function getResearchersFullName(allDataJson){
    var str = [];
    var myarr = [];
    var c = 0;
    for(var i = 0; i < allDataJson.length; i++){

        myarr[i] = allDataJson[i].Researchers.split(", ");

        for(var j = 0; j < myarr[i].length; j++){
            str[c] =  myarr[i][j];
            //console.log(str[c]); //prints as expected
        }
    }

    return str;
}

I am trying to use the returned value as follows but it only prints one of the str values.

var fullnames = getResearchersFullName(allDataJson);

        for(var i = 0; i <fullnames.length; i++){
                console.log(fullnames[i]); //returns only 1 object

        }

1 Answer 1

2

Your code never increments c. The only element of str that's ever modified is element 0.

Use str.push(myarr[i][j]); and you won't need c at all.

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

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.