Before going to answer read this Documentation once. Then you clearly understand the answer.
Try this It may work for you.
Object.keys(data.shareInfo[i]).length
Answer from James on Stack OverflowBefore going to answer read this Documentation once. Then you clearly understand the answer.
Try this It may work for you.
Object.keys(data.shareInfo[i]).length
First if the object you're dealing with is a string then you need to parse it then figure out the length of the keys :
obj = JSON.parse(jsonString);
shareInfoLen = Object.keys(obj.shareInfo[0]).length;
My question is pretty straight forward,
I am trying to grab the lenght of an array returned by my json. However, the array could be possibly two different names (for example orange or apple)
so how would I write code that would just grab the array length regardless of the array name
JSON array get length using jQuery or javascript - Stack Overflow
Size of JSON array
javascript - get size of JSON object - Stack Overflow
jquery - Cannot get the length of JSON array in javascript - Stack Overflow
This does the same think as Object.keys(obj).length, but without the browser compatibility issue (I think).
What about:
var obj = {
yo: {
a: 'foo',
b: 'bar'
}
hi: {
c: 'hello',
d: 'world',
e: 'json'
}
}
var arr = [], len;
for(key in obj) {
arr.push(key);
}
len = arr.length;
console.log(len) //2
OR
var arr = [], len;
for(key in obj.hi) {
arr.push(key);
}
len = arr.length;
console.log(len) //3
OR, in your case
var arr = [], len;
for(key in studentMockBeanMap) {
arr.push(key);
}
len = arr.length;
console.log(len); //4
You can also use the lodash library (which has a size function).
http://lodash.com/docs#size
_.size({red: 'red', blue: 'blue'}) // 2
You can use something like this
var myObject = {'name':'Kasun', 'address':'columbo','age': '29'}
var count = Object.keys(myObject).length;
console.log(count);
Your problem is that your phones object doesn't have a length property (unless you define it somewhere in the JSON that you return) as objects aren't the same as arrays, even when used as associative arrays. If the phones object was an array it would have a length. You have two options (maybe more).
Change your JSON structure (assuming this is possible) so that 'phones' becomes
"phones":[{"number":"XXXXXXXXXX","type":"mobile"},{"number":"XXXXXXXXXX","type":"mobile"}](note there is no word-numbered identifier for each phone as they are returned in a 0-indexed array). In this response
phones.lengthwill be valid.Iterate through the objects contained within your phones object and count them as you go, e.g.
var key, count = 0; for(key in data.phones) { if(data.phones.hasOwnProperty(key)) { count++; } }
If you're only targeting new browsers option 2 could look like this
The jList property of the object is just a string, so you need to convert it to a Javascript object using JSON.parse().
// Dummy of your "data" variable
var data = {"jList":"[{\"added_by\":\"Ani\",\"description\":\"example description.\",\"start_date \":\"2014-10-10\",\"mark\":255,\"id\":975},{\"added_by\":\"Ani\",\"description \":\"example description..\",\"start_date\":\"2014-10-10\",\"mark\":255,\"id\":980 }]"};
var myList = JSON.parse(data.jList);
alert(myList.length); // Alerts "2"
"jList":"[{\"added_by\":\"... is not an array, it's a string (and that's why it's length is 456 or 200 if you change the question).
Remove the surrounding double-quotation marks for it to be an array. Then you'll have Array.prototype.length.
The variable you are trying to store is already a json encoded string. You can not stringify it again.
You can drop those ' and reformat the json, then it would be a normal json, then you can call stringify on it.
var data = {"history":[{"keyword":key,"msg":msg,"ver":ver}]};
localStorage.setItem("history", JSON.stringify(data));
To get the length of the array after retrieve:
var historydata = JSON.parse(localStorage.getItem("history"));
console.log(historydata.history.length);
You have already a JSON string. After JSON.stringify you get a string from a string.
The array size is one, because the content in one object.
var key = 'KEY', msg = 'MSG', ver = 'VER',
data = '{"history":[' + '{"keyword":"' + key + '","msg":"' + msg + '","ver":"' + ver + '"}]}',
obj = JSON.parse(data);
document.write(obj.history.length + '<br>');
document.write('<pre>' + JSON.stringify(obj, 0, 4) + '</pre>');
Maybe you consider a better use of object, like an object literal:
var key = 'KEY', msg = 'MSG', ver = 'VER',
obj = { history: [{ keyword: key, msg: msg, ver: ver }] };
document.write('<pre>' + JSON.stringify(obj, 0, 4) + '</pre>');
Your JSON is not an array, but an object. If you want it to be an array, it should be something like this:
[
"{\"start\":{\"lat\":22.9939202,\"lng\":72.50009499999999},\"end\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"waypoints\":[[23.0316834,72.4779436]]}",
"{\"start\":{\"lat\":22.9999061,\"lng\":72.65318300000001},\"end\":{\"lat\":23.0420584,\"lng\":72.67145549999998},\"waypoints\":[[23.02237,72.6500747]]}",
"{\"start\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"end\":{\"lat\":22.9999061,\"lng\":72.65318300000001},\"waypoints\":[[23.0016629,72.58898380000005]]}"
]
Then, you can get a javascript array as follows:
var array = JSON.parse(jax.responseText);
And access values as follows:
array[0]
array.length
EDIT: In order to have a real JSON array with the PHP json_encode method, see this related question.
With this modification you will be able to use all the possibilities of JS array without workaround.
Objects in JavaScript don't have a .length property like Arrays do.
In ES5 you can do: Object.keys({}).length; // 0
The other solution would be to loop over all the properties of your object with a for .. in loop and count.
your data is already json format:do like this
var App = [
{
"id": "123",
"caption": "Test",
"description": "Test Desc"
},
{
"id": "345",
"caption": "adsasdasd",
"description": ""
},
{
"id": "456",
"caption": "adsasdasd",
"description": ""
},
{
"id": "578",
"caption": "adsasdasd",
"description": ""
}
];
console.log(App);
console.log(App[0].length);// you can not get length from this because it is not array it's an object now.
var AppLen = App.length;
alert(AppLen);
obj.size() or obj.length
If that dont run try this: Length of a JavaScript object
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
// Get the size of an object
var size = Object.size(myArray);