Matthew B
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(re sults).len gth);
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?
I have any ajax call that returns json data and i have tried:
console.log(results.length
Returns undefined
OR
console.log(Object.keys(re
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"}
It would be most helpful if you would provide a sample result set.
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
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
"[{"ClientID":3077,"SumToday":27,"LoadType":"Soil","Client_Name":"1"},{"ClientID":42,"SumToday":20,"LoadType":"Soil","Client_Name":"2"}]"
ASKER
Also reported this by accident lol
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
}
ASKER
Ok so i changed my controller to return JSON string:
then with JS i try:
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
results: "[{"ClientID":3077,"SumToday":3,"LoadType":"Soil","Client_Name":"1"}]"
then with JS i try:
var obj = JSON.parse(results);
console.log(obj);
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"
}
],
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you kind sir.
You are most welcome.