0

Well I've just discovered JSON today but I have a problem using it correctly. I really can't find a solution...

Basically, I just want to count the elements of my array (count all the dM), and wrap on a specific element (dM1 for example).

Here is my code so that you can understand: http://jsfiddle.net/dRycS/9/

4 Answers 4

3

Adding to what @Pointy said here is your code modified: JSFiddle Demo

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

var dMContent = {
    "dM1" : [
        {
            "name" : "EeEeEeEe",
            "link" : "http://test.com"
        },
        {
            "name" : "FfFfFfFf",
            "link" : "http://test.com"
        },
        {
            "name" : "GgGgGgGg",
            "link" : "http://test.com"
        } 
    ],
    "dM2" : [
        {
            "name" : "EeEeEeEe",
            "link" : "http://test.com"
        },
        {
            "name" : "FfFfFfFf",
            "link" : "http://test.com"
        } 
    ],
    "dM3" : [
        {
            "name" : "EeEeEeEe",
            "link" : "http://test.com"
        } 
    ] 
};

var STORAGE = JSON.stringify(dMContent);
var parsed = JSON.parse(STORAGE);


// WHAT I WANT TO DO

// Count the number of dM
console.log(Object.size(parsed));  //gives you 3

//display the content
for(var i in parsed){
    console.log('data in ' + i);
    for(var j=0; j<parsed[i].length; j++){
         console.log(parsed[i][j].name + ' ' + parsed[i][j].link);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

But it's better to use an array so no ? And how to show the content of an elem ?
@Sindar i have update the jsfiddle and at the bottom it shows you how to display your JSON. Basically i use for(var i in parsed) to deal with the object looping its like dealing with associative array
3

What you've got there is not an Array; it's an Object. Array objects do have a "length" property, but Objects do not.

It's not clear exactly what you want; if you wanted to count every property of every object inside of "dMContent", you'd write something to count recursively. For a single "layer" of an object, something like this might be what you want:

function objectSize(obj) {
  var count = 0;
  for (var k in obj) {
    if (obj.hasOwnProperty(k)) ++count;
  }
  return count;
}

5 Comments

+1 also his JSON is invalid in the jsfiddle here is the update jsfiddle.net/kjy112/dRycS/10
What i want is, know how many dM they are, here 3. And if i give dM1 ,know all his name and link.
Well the "size" of the outer object would be what that little function returns. To get the size of the "inner" objects, you'd just call the function and pass in one of those.
But it seems to be a complicated idea... Not better to simply use a real array in my case ?
@Sindar real arrays in JavaScript are indexed with integers, not strings.
0

In your code dMContent is an Object, not an Array.

To count elements in an Object, do this:

var i = 0;
for (x in parsed) {
    if (parsed.hasOwnProperty(x)) {
        i++;
    }
}
alert(i);

Comments

0

Try this: function objectCount(obj) {

objectcount = 0;
$.each(obj, function(index, item) {
    objectcount = objectcount + item.length;
});
return objectcount;
}

objectCount(obj); where obj is a json object with json array as sub objects

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.