Jimmy Obonyo Abor
Jimmy Obonyo Abor

Reputation: 7885

Iterate over json object having an object

I have the following json output from an ajax call ,

{17: {access: "1",id: "2"}} 

I'd like to iterate and get value of id , I am using below code but I get error of undefined in console .

$(obj).each(function(i, val) {
    console.log(val.id);
}); 

What am I doing wrong ?

Upvotes: 1

Views: 83

Answers (6)

CoderPi
CoderPi

Reputation: 13221

You can do it like this:

var obj = {17: {access: "1",id: "2"}} 

// Native JS
for (var k in obj) {
  console.log(obj[k].id)
}

// JQuery
// Documentaion: http://api.jquery.com/jquery.each/
$.each(obj, function(i, val) { 
  console.log(val.id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Hope it helps, any questions?

As requested I'd like to point out that the term "JSON-Object" doesn't realy exist. You are using a simple JavaScript-Object, wich can be made of an JSON-String 😇

Upvotes: 4

Johan Karlsson
Johan Karlsson

Reputation: 6476

You are making an jQuery object of your JSON and then iterating over that jQuery object. Take a look at the API docs of jQuery.each():

You are supposed to use it like this:

jQuery.each(object, callback)

Where object is the object to iterate over and callback is the callback function to be run for every key in object.

var obj = {
  17: {
    access: "1",
    id: "2"
  },
  18: {
    access: "2",
    id: "5"
  }
};

$.each(obj, function(i, val) {
  document.body.innerHTML += val.id + "<br>";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 0

Tamas Hegedus
Tamas Hegedus

Reputation: 29946

You have to use $.each(array, callback):

var obj = {
  17: { id: "2", access: "1" },
  23: { id: "8" }
};

$.each(obj, function(i, val) {
  console.log(val.id);
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386868

A solution with Object.keys() and Array.prototype.some() in a recursion style.

var object = { 17: { access: "1", id: "2" } };

function find(p, o) {
    var result;
    Object.keys(o).some(function (k) {
        if (k === p) {
            result = o[k];
            return true;
        }
        if (typeof o[k] === 'object') {
            result = find(p, o[k]);
        }
    });
    return result;
}

document.write('<pre>' + JSON.stringify(find('id', object), 0, 4) + '</pre>');

Upvotes: 0

Oleksandr T.
Oleksandr T.

Reputation: 77522

In this case you should use $.each from jQuery utils

var obj = {17: {access: "1",id: "2"}} 

$.each(obj, function(i, val) {
    console.log(val.id);
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 2

Venkat.R
Venkat.R

Reputation: 7746

Try the below code.

var obj = {17: {access: "1",id: "2"}};

if( $.isPlainObject(obj) ) {
  $(obj).each(function(i, val) {
    console.log(val.id);
}); 
}
else {
  console.log('invalid Object');
}

Upvotes: 0

Related Questions