0

Instead of looping through one time to show

id1
id2
id3

it loops through 3 times before stopping. what can i put to make it loop through only once.

html:

<p id="show_me"></p>
<button onclick="ObjectArray()">click me</button>

javascript:

var ObjectArray = function() {
        // object literal
        var id1 = {
            firstName: "John",
            lastName: "Doe",
            id: "12345"
        };

        // keyword new
        var id2 = new Object;
        id2.firstName = "Adam";
        id2.lastName = "Bakely";
        id2.id = "abcdef";

        // object constructor 
        function employee(first, last, id) {
            this.firstName = first;
            this.lastName = last;
            this.id = id;
        }
        var id3 = new employee("Dallas", "Star", "abc123");

        //create an array
        var IdArray = [id1, id2, id3];

        //for loop to display results
        var text="";
        var i;

        for (i = 0; i < IdArray.length; i++){
            text += IdArray[0].firstName + " " + IdArray[0].lastName + " " + IdArray[0].id + "<br>";
            text += IdArray[1].firstName + " " + IdArray[1].lastName + " " + IdArray[1].id + "<br>";
            text += IdArray[2].firstName + " " + IdArray[2].lastName + " " + IdArray[2].id + "<br>";
        }
        document.getElementById("show_me").innerHTML = text;
}

3 Answers 3

3

It iterates three times, because you loop for the length of the array, which has 3 items.

If you want to 'iterate' once, you can just omit the for loop:

text += IdArray[0].firstName + " " + IdArray[0].lastName + " " + IdArray[0].id + "<br>";
text += IdArray[1].firstName + " " + IdArray[1].lastName + " " + IdArray[1].id + "<br>";
text += IdArray[2].firstName + " " + IdArray[2].lastName + " " + IdArray[2].id + "<br>";

But I think you actually wanted to do this:

for (i = 0; i < IdArray.length; i++){
    text += IdArray[i].firstName + " " + IdArray[i].lastName + " " + IdArray[i].id + "<br>";
}

That way you use the loop what it's for: Iterate over an array of any length and repeat a piece of code for each item in the array.

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

Comments

0

Remove everything from the for loop, and add this instead:

text += IdArray[i].firstName + " " + IdArray[i].lastName + " " + IdArray[i].id + "<br>";

Comments

0

Every thing is fine ... but please replaced the following code

 for (i = 0; i < IdArray.length; i++){
        text += IdArray[i].firstName + " " + IdArray[i].lastName + " " + IdArray[i].id + "<br>";
        text += IdArray[1].firstName + " " + IdArray[1].lastName + " " + IdArray[1].id + "<br>";
        text += IdArray[2].firstName + " " + IdArray[2].lastName + " " + IdArray[2].id + "<br>";
    }

with

for (i = 0; i < IdArray.length; i++){
        text += IdArray[i].firstName + " " + IdArray[i].lastName + " " + IdArray[i].id + "<br>";

    }

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.