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

  • Home
  • SEARCH
  • 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 7767205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:37:04+00:00 2026-06-01T15:37:04+00:00

My goal is to iterate through an array of results and do stuff with

  • 0

My goal is to iterate through an array of results and do stuff with the data but for some reason I can’t get the foreach loop in javascript to work, I can’t seem to make any sort of enumeration work.

Here’s my test

console.debug("I got this far.");
for(var i=0;i< results.length; i++){
    console.debug("Worked.");
}
console.debug("Past.");
console.debug(results);

I have also tried

results.forEach(function(x) { console.debug("Worked"); });

and

for(var x in results)

None seem to be working, is there something I’m doing wrong?

The output for the above is

I got this far.  query.html:39
Past.            query.html:40
[ Object , Object , Object , Object , Object , Object , Object , Object , Object , Object ]

So I know that the results variable is the right type but it is just not iterating. There is another place where the forEach() works just fine but here it’s not, any suggestions on how to debug and address this would be helpful.

full code (WORKING SOLUTION)

var table;
var request = { query : { match_all : {}}};
var map;
function initialize(results) {
    var myOptions = {
      zoom: 8,
      center: new google.maps.LatLng(41.4, -71.3),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);
    results.forEach(function(x) {
      var markerLoc = new google.maps.LatLng(x.location.lat, x.location.lon);
      var marker = new google.maps.Marker({
        position: markerLoc,
        map: map,
        title: "Hello World!"
        });
      });

};
$(document).ready(function() {
        var results = [];
        $.ajax({
            url: "http://localhost:9200/devices/devices/_search",
            type: "POST",
            data: JSON.stringify(request),
            dataType: "json",
            beforeSend: function(x) {
                if ( x && x.overrideMimeType) {
                    x.overrideMimeType("application/j-son;charset=UTF-8");

                    }
            },
            success: function(data) { 
                data.hits.hits.forEach(function(x) { results.push(x._source); } );
                initialize(results);
            }

        });

});
  • 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-06-01T15:37:05+00:00Added an answer on June 1, 2026 at 3:37 pm

    You are passing results to initialize as soon as the Ajax request has been sent. You aren’t waiting for the response to come back, so the code that populates it doesn’t fire until too late.

    Move the call to initialize so it is inside the success handler.

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

Sidebar

Related Questions

I am looking to iterate through these data structures (basically a directory structure) which
Goal : I wants when I drag image it become fade so we can
Goal is to make a dialog that appears on menu_key pressed, but it keeps
Goal My goal is to to get Twitter Bootstrap to worth with my Play
Goal: order records by 'start_date' select first record get the id of the record
Goal: Suggest objects based on user's choices Data: Table containing info on how users
Goal To create an array of Model’s, managed by an ArrayController (ArrayProxy). Requirements Use
Goal: I want to implement a hard-coded lookup table for data that doesn't change
Goal: Based on todays date and month, get actual and the next two days
The goal is to treat a 1D array as a 2D grid. A second

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.