redGREENblue
redGREENblue

Reputation: 3126

Iterating through json values

Here's is the JSON I am talking about,

{
    "0": {
        "entry_id": "4",
        "category_id": "3",
        "title": "lorem ipusm"
    },
    "1": {
        "entry_id": "5",
        "category_id": "3",
        "title": "lorem ipusm dolor"
    },

    ......
    ......

    "total_entry": 270,
    "pending": 7,
    "url": "http://domainhere.tld/url"
}

I need to read the values of fields like total_entry, pending etc..and then also need to iterate through all the objects with keys like 0,1 (can go on, only two are shown in this example. This is what I am trying

var makelist;
for (var i = 0; i < objData.length; i++) {
    makelist='<li>'+ objData[i].title + '</li>';
}
$('#conatiner').append(makelist);

It works for getting the objects with keys like 0,1 but only when the json doesn't contain the other fields (total_entry etc..). How do I read both fields?

Upvotes: 0

Views: 82

Answers (6)

skobaljic
skobaljic

Reputation: 9614

Use jQuery each iterate function to iterate the object.

Description: A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

Than for each property/key that is not Not-a-Number add new list item:

var obj = {
    "0": {
        "entry_id": "4",
        "category_id": "3",
        "title": "Title for item 0"
    },
    "1": {
        "entry_id": "5",
        "category_id": "3",
        "title": "Title for item 1"
    },
    "total_entry": 270,
    "pending": 7,
    "url": "http://domainhere.tld/url"
};
var makelist = '';
$.each(obj, function( i, n ) {
    if ( !isNaN(parseInt(i)) ) makelist+='<li>'+ n.title + '</li>';
});
$('#container').append(makelist);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="container"></ul>

Upvotes: 2

Thalsan
Thalsan

Reputation: 5690

Use for (.. in ..), then you get all keys and not only the numerical. To check if the value is an object you can use typeof.

var makelist = '';
for (var i in obj) {
    if (typeof obj[i] == 'object')
        makelist='<li>'+ obj[i].title + '</li>';
    else 
        makelist += '<li>' + obj[i] + '</li>';
}   

$('#conatiner').append(makelist);

Upvotes: 0

Neil Shah
Neil Shah

Reputation: 181

var myJson = {
    "0": {
        "entry_id": "4",
        "category_id": "3",
        "title": "lorem ipusm"
    },
    "1": {
        "entry_id": "5",
        "category_id": "3",
        "title": "lorem ipusm dolor"
    },
    "50": {
        "entry_id": "50",
        "category_id": "30",
        "title": "lorem ipusm dolor number 50"
    },
    "total_entry": 270,
    "pending": 7,
    "url": "http://domainhere.tld/url"
}

//for all the keys in myJson
for (var k in myJson){

    //get 'title' if key is a number more than -1
    if (k > -1){
        console.log(myJson[k].title)
    }
    else{
        //get value of the key
        console.log(myJson[k])
    }
}

Upvotes: 1

Viswanath Donthi
Viswanath Donthi

Reputation: 1821

You can iterate through jquery's each function as:

var makelist;

$.each(objData, function(i, data){
     makelist = '<li>'+data.title+'</li>';
     $("#conatiner").append(makelist);
});

Upvotes: 0

RhinoDevel
RhinoDevel

Reputation: 712

This is how you get the values of all object's keys that are integers:

var obj = { '0': 'Zero', 1: 'One', '2': 'Two', somekey: 'somevalue', someotherkey: 'someothervalue' }, propName, arr = [];

for(propName in obj)
{
    if(obj.hasOwnProperty(propName)) // Avoids using keys/values from possibly existing prototype chain.
    {
        if(!isNaN(parseInt(propName, 10))) // Checks, if key name is a number by trying to parse and validating it.
        {
            arr.push(obj[propName]); // Adds key's value to array (as example).
        }
    } 
}

console.log(arr);

Just replace sample object with yours and replace the content of the inner if clause with your (makelist) code.

Upvotes: 0

yanchips
yanchips

Reputation: 76

You can use your loop to iterate thru the indexed items in the object and iterate thru the other properties of your object using this syntax:

for (var prop in objData) {
  if (objData.hasOwnProperty(prop) && isNaN(prop)) {
    // use objData[prop]
  }
}

The hasOwnProperty check will filter out any properties not directly contained in the object (inherited from a base class).

Upvotes: 0

Related Questions