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 1054595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:25:51+00:00 2026-05-16T17:25:51+00:00

If I have the following function that iterates through a json response object (val2):

  • 0

If I have the following function that iterates through a json response object (val2):

function test(val2){

    jQuery.each(val2.maps, function(j, val3) {    

        maps = new Array({

            id: val2.maps[j].id,
            parent: val2.maps[j].parent,
            image: val2.maps[j].image,
            data: val2.maps[j].data,
            width: val2.maps[j].width,
            height: val2.maps[j].height,
            top: val2.maps[j].top,
            left: val2.maps[j].left                         
        })

    });

return maps
}

how do I get it into an array that resembles the following format? Currently I’m only getting back the last array item.

maps = [{
    id: 'south',
    parent: 'us',
    image: '/resources/images/maps/map_south.jpg',
    data: 'popups/region_south.html',
    width: '227px',
    height: '177px',
    top: '120px',
    left: '49px'
},
{
    id: 'east',
    parent: 'us',
    image: '/resources/images/maps/map_east.jpg',
    data: 'popups/region_east.html',
    width: '156px',
    height: '121px',
    top: '120px',
    left: '283px'
}]

Cheers

  • 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-16T17:25:52+00:00Added an answer on May 16, 2026 at 5:25 pm

    Always fun when people post answers without explaining anything it seems to me that you don’t actually know what you’re doing here, so let me explain:

    function test(val2){
    
        // use jQuery.each to apply a function to each element in val2.maps
        jQuery.each(val2.maps, function(j, val3) {    
    
            // here you create a new global variable called 'maps' and assign and Array to it
            // the constructor 'new Array' is given one argument here
            // which is the object in which you map the values
            // notice: you're always creating a new array with only ONE element
            //         therefore you never fill it up
            maps = new Array({
    
                // Instead of 'val2.maps[j].id', you could simply do 'val3.id' etc.
                // Since val3 already contains 'val2.maps[j]'
                id: val2.maps[j].id,
                parent: val2.maps[j].parent,
                image: val2.maps[j].image,
                data: val2.maps[j].data,
                width: val2.maps[j].width,
                height: val2.maps[j].height,
                top: val2.maps[j].top,
                left: val2.maps[j].left                         
            }) // missing semicolon here btw ;)
    
        });
    
        // this returns the last Array that was created
        return maps
    }
    

    Here’s a fixed version, which actually does populate the array properly:

    function test(val2){
        var maps = []; // create an array that's local to this function, so it does not override any global stuff
        jQuery.each(val2.maps, function(j, val3) {    
    
            // append a new element to the array, this time directly use 'val3'
            maps.push({
                id: val3.id,
                parent: val3.parent,
                image: val3.image,
                data: val3.data,
                width: val3.width,
                height: val3.height,
                top: val3.top,
                left: val3.left                         
            });
        });
    
        // this time return the array with all the elements
        return maps
    }
    

    Also all your code has basically no effect, since all you’re doing is copying the elements of one array into another one without changing the structure in any way.

    So in the end the values in both val2.maps and your returned maps are “identical”, only difference is that they do not point to the same objects since you copied all the values into new ones.

    If you don’t need a copy or you want to keep the reference to the original values, this would do it:

    function test(val2) {
        return val2.maps; // return the reference to maps
    }
    

    Nick has shown an even fancier version of doing the whole copying thing, but I thought it might help you more if someone would actually point out your mistakes, rather then posting even “crazier” jQuery code for you to copy&paste 😉

    PS: Love my Firefox, Session just crashed but my answer was still here after the restart ^_^”

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

Sidebar

Related Questions

I have tried to make a function that iterates through the following array to
I have the following jquery function that submits to an iframe. The message sent
I have the following which iterates a function across all textboxes (type=text): $('input[type=text]').each(function ()
I have the following function that works properly in Chrome and Firefox, but not
I have the following function that does string splitting: on splitText(aString, delimiter) set retVal
I have the following function that deletes the LaTeX command surrounding the current cursor
I have the following function that I am using to remove the characters \04
I have the following function that takes a number like 5 and creates a
I have the following function that retrieves an image from a twitter feed, the
I have the following function that is supposed to trigger anytime one of the

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.