Get length of Javascript object

Matthew B
Matthew B used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sam JacobsDirector of Technology Development, IPM

Commented:
It would be most helpful if you would provide a sample result set.
Matthew BHack

Author

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 BHack

Author

Commented:
Also reported this by accident lol
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Sam JacobsDirector of Technology Development, IPM
Commented:
The json provided above contains a single element ... an array (delimited by the square brackets) containing 2 items (delimited by the curly brackets).
Matthew BHack

Author

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, IPM

Commented:
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 BHack

Author

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, IPM

Commented:
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

Hack
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);
Matthew BHack

Author

Commented:
Thank you kind sir.
Sam JacobsDirector of Technology Development, IPM

Commented:
You are most welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial