-3

I want to iterate this kind of JSON:

{
  "object": {
    "array1": {
      "id": 1
    },
    "array2": {
      "id": 2
    }
  }
}

I've attempted to use this:

for (var i in dictionary) {
  console.log(dictionary[i].id);
}

but doesn't work.

5
  • 6
    Do people not read documentation and tutorials anymore? learn.jquery.com/using-jquery-core/iterating Commented Dec 16, 2016 at 12:15
  • jsfiddle.net/jzxe1a6b Commented Dec 16, 2016 at 12:22
  • @epascarello Reopening because it is clear now. Commented Dec 16, 2016 at 12:28
  • @Craig If that's the case, if you suspect, I am very well happy to vote to delete this question. But the OP has lot more questions, that's been discussed below. Check the discussion below. Commented Dec 16, 2016 at 12:30
  • 2
    @Craig Voted to delete. Happy now? Commented Dec 16, 2016 at 12:40

3 Answers 3

3

I've searched everywhere and can't find an answer that I want. Seriously? Didn't see what $.each does?

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.

var arr = {
  "object": {
    "array1": {
      "id": 1
    },
    "array2": {
      "id": 2
    }
  }
};
$.each(arr, function (i, v) {
  console.log(i);
  console.log(v);
  $.each(v, function (idx, val) {
    console.log(idx);
    console.log(val);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

AJAX

If you are using AJAX to fetch the JSON, you can very well use: $.getJSON:

$.getJSON("https://cdn.rawgit.com/fge/sample-json-schemas/master/avro/avro-schema.json", function (res) {
  $.each(res, function (i, v) {
    console.log(i);
    console.log(v);
    if (typeof v == "object")
    $.each(v, function (idx, val) {
      console.log(idx);
      console.log(val);
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

9 Comments

Yours works, @Craigs doesn't.
I'll accept when I can.
@decryptable Thanks.
@Craig Nopes, I reopened because it is clear now.
@Craig I have added that in the question's comment as well.
|
-1
var objectData = {
  "object": {
    "array1": {
      "id": 1
    },
    "array2": {
      "id": 2
    }
  }
};

$.each(objectData, function(key, data){
    // Do stuff here
});

To use this with json from a URL:

$.ajax({
    url: URL,
    dataType: 'jsonp',
    success: function(json) {
       $.each(json, function(key, data){
           console.log(key);
           console.log(data);
        });
    }
});

9 Comments

I'll give this one a go.
Sorry, I just edited it
@Craig why don't you give an explanation of what it does?
Because its clear that it is doing "each - objectData" with a key and data value?
How do I got by this if I'm pulling the JSON from a URL?
|
-1

try the following

var d={
  "object": {
    "array1": {
      "id": 1
    },
    "array2": {
      "id": 2
    }
  }
}

$.each(d,function(i,v){console.log(i,v)})

1 Comment

Another LQ answer without any explanation. Worse than the previous two answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.