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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:56:25+00:00 2026-06-13T16:56:25+00:00

I have two pieces of data in a multi-level array (object?) that I’m referencing

  • 0

I have two pieces of data in a multi-level array (object?) that I’m referencing in a callback function set with 2 for loops. However, I can’t figure out how to get the current loop value into the callback: it uses the last value for all callbacks.

for (k in myobj.myarr) {
    for (m in myobj.myarr[k]){
        document.addEventListener(
            k,
            function(event){
                myobj.myfn(event, myobj['myarr'][k][m][0], myobj['myarr'][k][m][1]);
            },
            true
        );
    }
}

Where myobj['myarr'][k][m][0] are strings and myobj['myarr'][k][m][1] are functions. I have a console.log in the functions, and the same function is always called (the last one).

Yes, this is probably an odd implementation, and no, I can’t use libraries. How do I get the correct values out of my array to pass into the callback functions?

  • 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-13T16:56:27+00:00Added an answer on June 13, 2026 at 4:56 pm

    What you really want is m bound to a context known only to one listener. For clarity’s sake, you could define a function like this:

    function doEvent(k, myobj, m) {
      document.addEventListener(
        k,
        function(event){
          myobj.myfn(event, myobj['myarr'][k][m][0], myobj['myarr'][k][m][1]);
        },
        true
      );
    }
    

    …and call it for each object in your array:

    for (k in myobj.myarr) {
      for (m in myobj.myarr[k]){
          doEvent(k, myobj, m);  
      }
    }
    

    Now, the events will be specific to their respective ms in the loop.

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

Sidebar

Related Questions

I have two pieces of data: 1) User and 2) Portfolio. Each user has
I have two pieces of javascript that I would like to work together. I
I have a a loop that loops through data, what I am trying to
If I have two pieces of character data, what is the best way to
I have two pieces of code that I need to use together. I am
I have a list of tuples pairing two pieces of data... I'd like to
I have two very similar pieces of ASP.NET code that send a file in
I have a requirement to return two different pieces of string data through RPC
I have two threads. The producer is producing pieces of data (String objects), where
I have two pieces of C++ code running on 2 different cores. Both of

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.