Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:22:38+00:00 2026-05-28T05:22:38+00:00

{ Adam:{ Math:93, Science:96, Email:adam@example.com, City:NY }, Tom:{ Math:65, Science:56, Email:tom@example.com, City:LA }, Joe:{

  • 0
{
   "Adam":{
      "Math":"93",
      "Science":"96",
      "Email":"adam@example.com",
      "City":"NY"
   },
   "Tom":{
      "Math":"65",
      "Science":"56",
      "Email":"tom@example.com",
      "City":"LA"
   },
   "Joe":{
      "Math":"74",
      "Science":"83",
      "Email":"joe@example.com",
      "City":"Washington"
   }
}

Above is the JSON content present at the http: //ActualUrl/path.json

I am accessing the JSON file and filling the two arrays with name and marks in science with the code below.

     var names=[];
     var marks=[];
    $.getJSON("http://path.json",function(data){               
                $.each(data, function(key, val) {   
                     names.push(key);
                    // I placed alert the check the value key.
            marks.push(parseInt(val.Science));                  
                    });
                });
    alert(names.toString()); // names is found blank
    alert(marks.toString()); 

When I check the array at the end. Array is found blank. Why this wrong behaviour with getJSON ? I placed alert in the each and check the value. It returns correct value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:22:39+00:00Added an answer on May 28, 2026 at 5:22 am

    Primary problem

    You are filling your array in the async callback, but alert them right away. So, this is what happens:

    1. You execute ajax call. Request is sent to the server.

    2. You alert values of arrays. They are blank, since the server didn’t reply yet.

    3. Server replies, your callback is fired and arrays are filled. Too late.

    Solution:

    Put alert logic to the callback.

    Other notes

    jQuery.each is for iterating collections. You, on the other hand, have an object (or dictionary).

    Try this code:

    for(key in data) {
      names.push(key);
      var val = data[key];
      marks.push(parseInt(val.Science));                  
    }
    

    for ... in ... construct iterates object properties, which is what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm walking through Adam Kinney's Blend tutorials ( http://visitmix.com/labs/rosetta/EyesOfBlend/ ) and I'm seeing some
List_name Email ========== ================== andrew adam@gmail.com adam adam@gmail.com smith adam@gmail.com john adam@gmail.com andrew andrew@gmail.com
Based on this post http://www.adam-bien.com/roller/abien/entry/ejb_3_1_killed_the I use in my app @Named @Stateless bean for
I'm working on an implementation of FullCalendar by Adam Shaw (http://arshaw.com/fullcalendar/). Each user has
package com.adam.etutorial { import flash.display.MovieClip; import flash.text.TextField; import flash.display.Sprite; import flash.text.TextFormat; import flash.display.Shape; public
string '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg' (length=85) what i need is just http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg what is the best way
For example, let's say I have to dictionaries: d_1 = {'peter': 1, 'adam': 2,
I have a question about the examples in this article: http://code.google.com/appengine/articles/transaction_isolation.html Suppose I put
I have the following table which I'll call 'example' id name last_name 01 Adam
<title>Webisode 200 Playlist</title> <item> <title>Testing 201</title> <media:credit role=author>Adam Houston</media:credit> <media:content url=http://www.youtube.com/watch?v=gb0bGZ06vXn type=video/x-flv /> <jwplayer:duration>200</jwplayer:duration>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.