-2

I was told that the reason I had to loop through JSON data is because JSON is an array. If that is true my question is why do you have to loop through JSON data in order to retrieve JSON Values? If that is not true is there another way to retrieve JSON data using jQuery? I added jQuery to my tag words because I would like to retrieve JSON data via jQuery. My JSON data is below:

    [  
       {  
          "input":0,
          "candidate":0,
          "delivery":"one",
          "last":"two",
          "point":"none",
          "fruit":{  
             "apples":"yes",
             "oranges":"no",
             "grapes":"yes",
          },
          "analysis":{  
             "code":"Y",
             "crr":"TTB",
          }
       }
    ]
12
  • 3
    Your data is an array - and to find specific values in an array, you have to loop and find them (or use some array extension method, such as find - but under the hood that's just looping as well) - it's how arrays work. Commented Dec 21, 2017 at 16:22
  • 3
    JSON is a string representation of some data. That data may or may not contain an array or arrays. If you've been told that "JSON is an array" then you've been misinformed. Commented Dec 21, 2017 at 16:24
  • 1
    Have a look at Access / process (nested) objects, arrays or JSON Commented Dec 21, 2017 at 16:28
  • 2
    @Mariton you're on the right track. What you shared above is an example of a JSON array (determined by the square brackets [ and ]). Attributes inside maps can be accessed without iteration (maps are denoted using curly brackets { and }). Commented Dec 21, 2017 at 16:55
  • 1
    @Mariton I'm not trying to mock or insult and I apologize if I gave that impression: I was trying to explain via analogy that your question has a flawed premise. To do a waaay deeper dive than is necessary, computers store memory at physical memory addresses. Think of your computers memory as a neighborhood, and skipping several layers of indirection, when you access an array index or an object property/hashtable key the computer accesses the memory stored in the block related to the array/object (think street) at the offset of the index/key (think address). So if you don't know the exact... Commented Dec 21, 2017 at 20:29

1 Answer 1

-6

JSON stands for JavaScript Object Notation.

In JavaScript Objects are Associative Arrays. Associative Arrays map unique keys (could be a string, but could also be a number) to a value.

Normal arrays are actually subsets of Associative Arrays (they have only* number keys). Where each (unique) number maps to a value.

To get all the data out of an array you ask it for the value of each key. This is usually done with a loop. In the case of Associative Arrays that means looping over its keys (whatever type they might be), and asking for its value in the array, like so:

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}

*Not completely true in JS, as length and some other things are actually also keys, which is also related to why you have to ask if a key is it's own property.

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

11 Comments

1. Arrays and hashmaps are entirely different data structures, even in JavaScript. One is not a subset of the other. 2. Looping over an array with a for..in loop is asking for subtle bugs and has been considered bad practice for at least the last 6 years.
@JaredSmith I'm well aware that they are different data structures, but in general arrays are implementation in JS as special versions of hashmaps. For a couple of examples See the Mozilla Rhino implementation of an array at: [github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/…. The comment on line 2121 describes the overall strategy, where dense arrays use an actual underlying array, but anything else uses HashTable storage. Spider Monkey at: [hg.mozilla.org/mozilla-central/file/tip/js/src/vm/… takes a similar approach.
@user602607 implementation details !== semantics. Or performance. Or aid in correctness.
@JaredSmith in all due respect, while I was clearly wrong, and am indeed reflecting on how to create proper answers... I also feel that conversation is healthy, and the only way to properly resolve misunderstandings. I would also bring up these points in a work environment. Thank you for your time, I will stop now.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.