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

Matthew BHackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sam JacobsDirector of Technology Development, IPMCommented:
It would be most helpful if you would provide a sample result set.
Matthew BHackAuthor Commented:
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

Matthew BHackAuthor Commented:
Also reported this by accident lol
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

Sam JacobsDirector of Technology Development, IPMCommented:
The json provided above contains a single element ... an array (delimited by the square brackets) containing 2 items (delimited by the curly brackets).
Matthew BHackAuthor Commented:
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

Sam JacobsDirector of Technology Development, IPMCommented:
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

Matthew BHackAuthor Commented:
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
Sam JacobsDirector of Technology Development, IPMCommented:
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

Matthew BHackAuthor Commented:
Figured it out thanks to your help that its not valid json.

The issue was my controller, i was already sending valid json to the front end, but i was encapsulating that json in another json function

my controller

        $results = json_encode($results,true);       

        return response()->json(['results' => $results);

Open in new window


i changed it to

        $results = json_encode($results,true);
        return response($results);

Open in new window


and i am returning a valid json object, tested by console.log(results.length);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Matthew BHackAuthor Commented:
Thank you kind sir.
Sam JacobsDirector of Technology Development, IPMCommented:
You are most welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.