0

I am trying to navigate through a json object to return 1 element at a time and iterate through. Here's the JSON.

{
  "noun": {
    "syn": [
      "leap",
      "saltation",
      "startle",
      "start",
      "parachuting",
      "jumping",
      "actuation",
      "descent",
      "inborn reflex",
      "increase",
      "innate reflex",
      "instinctive reflex",
      "physiological reaction",
      "propulsion",
      "reflex",
      "transition",
      "unconditioned reflex"
    ]
  },
  "verb": {
    "syn": [
      "leap",
      "bound",
      "spring",
      "startle",
      "start",
      "leap out",
      "jump out",
      "stand out",
      "stick out",
      "rise",
      "climb up",
      "jump off",
      "derail",
      "chute",
      "parachute",
      "jumpstart",
      "jump-start",
      "pass over",
      "skip",
      "skip over",
      "alternate",
      "alter",
      "appear",
      "assail",
      "assault",
      "attack",
      "change",
      "climb",
      "dive",
      "drop",
      "enter",
      "go",
      "leave out",
      "locomote",
      "look",
      "miss",
      "mount",
      "move",
      "neglect",
      "omit",
      "overleap",
      "overlook",
      "participate",
      "plunge",
      "plunk",
      "pretermit",
      "seem",
      "set on",
      "shift",
      "start up",
      "switch",
      "travel",
      "vary",
      "wax"
    ],
    "rel": [
      "leap out",
      "jump on"
    ]
  }
}

Let's say I wanted to access "leap." It's two layers in. How would I 1) return leap, and 2) iterate to the next word?

Swaraj Giri
  • 4,007
  • 2
  • 27
  • 44
  • assuming your JSON object is in `obj`, `obj.noun.syn[0]` should do the trick – Lukman Jun 09 '16 at 04:03
  • I should have mentioned; I want to store the word inside a variable, and update the variable as I iterate through the array. – Robby Schlesinger Jun 09 '16 at 04:04
  • have a look at this http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Manish Jun 09 '16 at 04:04
  • 1
    Possible duplicate. Check out this answer. http://stackoverflow.com/questions/14150642/navigating-through-json-with-javascript – Dale Jun 09 '16 at 04:05
  • well, `obj.noun.syn` is an array that you can iterate just like you normally do. – Lukman Jun 09 '16 at 04:05
  • If you want to iterate over the obj.noun.syn and obj.verb.syn and do some processing to the words inside, you can take a look at my answer. It will work for any number of items like `noun` and `verb` inside your hash. – phreakv6 Jun 09 '16 at 04:20

2 Answers2

0

Is this what you are looking for?

Object.keys(my_hash).forEach(function (key) { 
    Object.keys(my_hash[key]).forEach(function (key2) { 
        for(var i=0;i<my_hash[key][key2].length;i++) {
            // Process here - You have access to all your words!
            console.log(my_hash[key][key2][i]);
        }
    })
})
phreakv6
  • 2,135
  • 1
  • 9
  • 11
0

I thought you wanna do something like this.

//Your array
var array = {"noun":{"syn":["leap","saltation","startle","start","parachuting","jumping","actuation","descent","inborn reflex","increase","innate reflex","instinctive reflex","physiological reaction","propulsion","reflex","transition","unconditioned reflex"]},"verb":{"syn":["leap","bound","spring","startle","start","leap out","jump out","stand out","stick out","rise","climb up","jump off","derail","chute","parachute","jumpstart","jump-start","pass over","skip","skip over","alternate","alter","appear","assail","assault","attack","change","climb","dive","drop","enter","go","leave out","locomote","look","miss","mount","move","neglect","omit","overleap","overlook","participate","plunge","plunk","pretermit","seem","set on","shift","start up","switch","travel","vary","wax"],"rel":["leap out","jump on"]}};

//The list you want to iterate
var words = array.noun.syn;

//Iterating in the array
    for (i=0; i<words.length; i++) {
        //Words[i] is every value return after leap (including itself)
        console.log(words[i]);
    }
Cesar Jr Rodriguez
  • 1,691
  • 4
  • 22
  • 35