How do I get total number of objects in a variable only once?

KPO

I am new to JS so struggling with this a bit.

    $(function () {
        $.ajax({
            url: "some_url",
            dataType: 'jsonp',
            success: function (whatever) {
                var allText = whatever.data._shared.text;
                for (var i = 0; i < allText.length; i++) {
                    var text1 = allText[i];
                    var testHtml = "";
                    testHtml += "";
                    testHtml += "";
                    testHtml += "";
                    $("#text").append(testHtml);
                    document.write(allText.length);
                }

            }

        });

    });

In a variable I want the total count of objects returned (allText = whatever.data._shared.text).

I tested out by using document.write(allText.length); and I am getting the right number but the result looks like this:

2020202020202020202020202020202020202020

So it appears to be repeating. How can I get the actual value, which is 20, inside a variable only once instead of it repeating?

Volune

You call document.write(allText.length); inside the for loop, so one time per iteration (so you write it 20 times). Try to call it outside the for loop:

var allText = whatever.data._shared.text;
for (var i = 0; i < allText.length; i++) {
    var text1 = allText[i];
    var testHtml = "";
    testHtml += "";
    testHtml += "";
    testHtml += "";
    $("#text").append(testHtml);
}
document.write(allText.length);

Also try to use your browser's debugger (go step by step) to understand what happened.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I write an index to get the total number of objects in a collection across documents in RavenDB?

From Dev

How do I write an index to get the total number of objects in a collection across documents in RavenDB?

From Dev

How do I instantiate a variable in C++ only once?

From Dev

How do I get the total number of distinct values in a column in a CSV?

From Dev

How do I use a Scanner only once then get rid of it?

From Dev

How do I get setInterval to only run once?

From Dev

How do I get the total number of records before I run limit when using Aggregation

From Dev

How do i get number of rows returned by a query in recordset variable?

From Dev

How do I pass variable number of arguments to GET in WebAPI

From Dev

How do I execute code once and only once in Swift?

From Dev

In Vim, how do I get the total number of matches after doing a search?

From Dev

How do i get the total average?

From Dev

How can I get a count of the total number of digits in a number in TypeScript?

From Dev

How do i parse the number only from item and put the number in int variable?

From Dev

How do I get Linq to return identical repeated rows only once?

From Dev

How do I set a variable to be the total count of values of another variable?

From Dev

How can i use the variable graphics only once?

From Dev

How do I find the total number of JSON entries?

From Dev

How do I calculate total number of unique values in a dataframe?

From Dev

How do I validate the total number of passed arguments?

From Dev

How do I calculate total number of unique values in a dataframe?

From Dev

How do I validate the total number of passed arguments?

From Dev

How do I display the number of total responders to a poll?

From Dev

how to I get the total number of stack frames used in recursion in Python?

From Dev

How can I get the total number of elements between two iterators?

From Dev

How can I get the total number of rows in a CSV file with PHP?

From Dev

How can I get the total number of documents found using the Nest?

From Dev

How to get number total of word that i explode using php?

From Dev

How can I get the total number of pages on Spring Data?

Related Related

  1. 1

    How do I write an index to get the total number of objects in a collection across documents in RavenDB?

  2. 2

    How do I write an index to get the total number of objects in a collection across documents in RavenDB?

  3. 3

    How do I instantiate a variable in C++ only once?

  4. 4

    How do I get the total number of distinct values in a column in a CSV?

  5. 5

    How do I use a Scanner only once then get rid of it?

  6. 6

    How do I get setInterval to only run once?

  7. 7

    How do I get the total number of records before I run limit when using Aggregation

  8. 8

    How do i get number of rows returned by a query in recordset variable?

  9. 9

    How do I pass variable number of arguments to GET in WebAPI

  10. 10

    How do I execute code once and only once in Swift?

  11. 11

    In Vim, how do I get the total number of matches after doing a search?

  12. 12

    How do i get the total average?

  13. 13

    How can I get a count of the total number of digits in a number in TypeScript?

  14. 14

    How do i parse the number only from item and put the number in int variable?

  15. 15

    How do I get Linq to return identical repeated rows only once?

  16. 16

    How do I set a variable to be the total count of values of another variable?

  17. 17

    How can i use the variable graphics only once?

  18. 18

    How do I find the total number of JSON entries?

  19. 19

    How do I calculate total number of unique values in a dataframe?

  20. 20

    How do I validate the total number of passed arguments?

  21. 21

    How do I calculate total number of unique values in a dataframe?

  22. 22

    How do I validate the total number of passed arguments?

  23. 23

    How do I display the number of total responders to a poll?

  24. 24

    how to I get the total number of stack frames used in recursion in Python?

  25. 25

    How can I get the total number of elements between two iterators?

  26. 26

    How can I get the total number of rows in a CSV file with PHP?

  27. 27

    How can I get the total number of documents found using the Nest?

  28. 28

    How to get number total of word that i explode using php?

  29. 29

    How can I get the total number of pages on Spring Data?

HotTag

Archive