1

Been struggling with counting through this array. I was able to do it previously but I don't think I quite understand how the parameters of a function are assigned its data.

I am bringing in an array with from JSON data over AJAX, then using a .each loop to go through the data and count it. But I can't seem to count it when using the index parameter. It is just giving me the actual objects and not counting how many objects there are.

I was hoping that someone could help me understand why I am getting the object and not the count of objects in the array. What I have now is just my final attempt at getting it to count, I know it's wrong.

I am trying to count how many "results" there are in the array (that came from the JSON file).

I have added snippets of my code and attached a link to the JSON file. I have also marked the problem area with a comment saying Problem Area

CODE -

$.getJSON('http://api.fixer.io/latest?base=ZAR', {
        action: "query", 
        list: "search",
        format: "json",
}
        , function (data) {
            var baseCurr = data.base;
            var baseDate = data.date;
            $('#curr-cont').append('<div class="base row1" id="row1"><div class="base flag" id="flag"><i class="famfamfam-flag-za"></i></div><div class="base country-name"><p class="c-name" id="count-name">' + baseCurr + '</p></div><div class="base currency"><p class="c-amount" id="curr">' + baseDate + '</p></div></div>');

           //***Problem Area***
    
            var rates = [data];
    
            $.each(rates[0], function (i, obj) {
               console.log(obj); 
            });

            $.each(data.rates, function (i, item) {
                var amount = [item];
                var name = [i];
                var maxLength = 4;
                var string = amount.toString();
                string = string.substr(0, maxLength);
                // amount = amount.substr(0, maxLength);
                
                $('#curr-cont').append('<div class="row1" id="row1"><div class="flag" id="flag"><i class="famfamfam-flag-' + name + '"></i></div><div class="country-name"><p class="c-name" id="count-name">' + name + '</p></div><div class="currency"><p class="c-amount" id="curr">' + string + '</p></div></div>');
                
//                if(i > 0){
//                    $('#list1').append('<li>' + name + '</li>');
//                }
//                else{
//                    $('#list2').append('<li>' + name + '</li>');
//                }
                
                
            });
    
    });

JSON Data File

JAAulde
  • 19,250
  • 5
  • 52
  • 63
  • I don't see you counting anything in that code, where would you assume the counting to happen? – jfornoff Mar 23 '17 at 12:51
  • Yes that's the problem. I am trying to count the amount of rates there are in the JSON file. –  Mar 23 '17 at 12:53
  • If it's truly and Array then use https://www.w3schools.com/jsref/jsref_length_array.asp . If it's an Object then use http://stackoverflow.com/questions/5223/length-of-a-javascript-object – bassxzero Mar 23 '17 at 12:54
  • 1
    http://jsbin.com/semuhecube/edit?js,console,output – bassxzero Mar 23 '17 at 12:56
  • I see that that works, but in that format.. How would I create the str variable with the getJSON at the top? –  Mar 23 '17 at 13:00
  • I keep getting - Uncaught SyntaxError: Unexpected token o in JSON at position 1 –  Mar 23 '17 at 13:05

1 Answers1

0

edit: since rates is an object and not an array, you can do: Object.keys(data.rates).length.

Object.keys(...) will give you an array with all the keys in the object.

original:

If you want to know the number of rates in that file: data.rates.length will give you the length of the rates Array that is returned in the data. No need to count it

DoXicK
  • 4,784
  • 24
  • 22