Link to home
Start Free TrialLog in
Avatar of Matthew B
Matthew BFlag for Canada

asked on

Get length of Javascript object

How come i cant get the length of this object/array in javascript?

I have any ajax call that returns json data and i have tried:

console.log(results.length);

Returns undefined

OR

console.log(Object.keys(results).length);

Returns 1  even though there are 6 items.

In my console when i console.log(result);

I see result: Array(6);

What am i doing wrong?

{results: Array(6)}
results: Array(6)
0: {ClientID: 3077, SumToday: 3, Client_Name: "1"}
1: {ClientID: 3089, SumToday: 2, Client_Name: "2"}
2: {ClientID: 2067, SumToday: 2,  Client_Name: "3"}
3: {ClientID: 3085, SumToday: 1,  Client_Name: "4"}
4: {ClientID: 2072, SumToday: 1, Client_Name: "5"}
5: {ClientID: 41, SumToday: 1,  Client_Name: "6"}

Open in new window

Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

It would be most helpful if you would provide a sample result set.
Avatar of Matthew B

ASKER

I have added it in.

I also tried to encode with php json_encode($results, true); and pass to JS in json object string but still console.log(results.length) doesnt work

"[{"ClientID":3077,"SumToday":27,"LoadType":"Soil","Client_Name":"1"},{"ClientID":42,"SumToday":20,"LoadType":"Soil","Client_Name":"2"}]"

Open in new window

Also reported this by accident lol
SOLUTION
Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I figured that. So if i i have the original json object, why cant i get this length or at least iterate over it? I tried forEach jquery and i get an error that results.forEach is not a function

{results: Array(6)}
results: Array(6)
0: {ClientID: 3077, SumToday: 3, LoadType: "Soil", Client_Name: "1"}
1: {ClientID: 3089, SumToday: 2, LoadType: "Soil", Client_Name: "2"}
2: {ClientID: 2067, SumToday: 2, LoadType: "Soil", Client_Name: "3"}
3: {ClientID: 3085, SumToday: 1, LoadType: "Soil", Client_Name: "4"}
4: {ClientID: 2072, SumToday: 1, LoadType: "Soil", Client_Name: "5"}
5: {ClientID: 41, SumToday: 1, LoadType: "Soil", Client_Name: "6"}
length: 6

Open in new window

The above is not a valid json object. Here is sample json:
{
  "orderID": 12345,
  "shopperName": "John Smith",
  "shopperEmail": "johnsmith@example.com",
  "contents": [
    {
      "productID": 34,
      "productName": "SuperWidget",
      "quantity": 1
    },
    {
      "productID": 56,
      "productName": "WonderWidget",
      "quantity": 3
    }
  ],
  "orderCompleted": true
}

Open in new window

Ok so i changed my controller to return JSON string:

results: "[{"ClientID":3077,"SumToday":3,"LoadType":"Soil","Client_Name":"1"}]"

Open in new window


then with JS i try:          

 var obj = JSON.parse(results);

            console.log(obj);

Open in new window


and get:

 Uncaught SyntaxError: Unexpected token o in JSON at position 1

I am obviously missing something completely, i work mostly with sql and php sorry i am noobie with JS
Still not valid json ...  your output should look something like this:

{
  "results": 6,
  "clients": [
    {
      "clientID": 3077,
      "SumToday": 3,
      "LoadType": "Soil",
	  "Client_Name" : "1"
    },
    {
      "clientID": 3089,
      "SumToday": 2,
      "LoadType": "Soil",
	  "Client_Name" : "2"
    },    
    {
      "clientID": 2067,
      "SumToday": 2,
      "LoadType": "Soil",
	  "Client_Name" : "3"
    },
    {
      "clientID": 3085,
      "SumToday": 1,
      "LoadType": "Soil",
	  "Client_Name" : "4"
    },
    {
      "clientID": 2072,
      "SumToday": 1,
      "LoadType": "Soil",
	  "Client_Name" : "5"
    }, 
   {
      "clientID": 41,
      "SumToday": 1,
      "LoadType": "Soil",
	  "Client_Name" : "6"
    }
  ],
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you kind sir.
You are most welcome.