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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:41:46+00:00 2026-06-05T17:41:46+00:00

I have an object and an array: m = { 1: [2, 3], 6:

  • 0

I have an object and an array:

m = { "1": ["2", "3"], "6": ["4", "5"] };
var p = ["1", "6"];

I have a for loop:

for (var i = 0; i < p.length; i++) {
    // Return an array that is the value of a key in m, for each key specified in p
    var array = m[p[i]]; 

    // do stuff with array
}

Any reason why the above does not work? array is still undefined after the for loop runs.

  • 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-05T17:41:48+00:00Added an answer on June 5, 2026 at 5:41 pm

    Your declaration of m = { "1": ["2", "3"], "6", ["4", "5"] }; gives syntax error for me. I assume you mean m = { "1": ["2", "3"], "6": ["4", "5"] };.

    p.length is 2, so you have 2 iterations of the loop. In first iteration values of your expressions are:

    i = 0
    p[i] = "1"
    m[p[i]] = m["1"] = ["2", "3"]
    

    In second loop:

    i = 1
    p[i] = "2"
    m[p[i]] = m["2"] (undefined)
    

    You have only m["1"] and m["6"], no m["2"]. That’s why array is undefined in the last iteration. So it remains undefined after the loop.

    You may correct m declaration as following:

    m = { "1": ["2", "3"], "2": ["4", "5"] };
    

    Now you will get array = ["4", "5"] after the loop.

    I can advise you not to store integers in strings. Use 2 instead of "2". Otherwise it can cause errors in the future. For example, 2 + 2 = 4 and "2" + "2" = "22". If you have "2" from some other code, use parseInt to convert it to a normal number.

    Also, you don’t have to create p variable with list of keys. You can simply use for..in loop to iterate through keys of your object:

    m = { 1: [2, 3], 2: [4, 5] };
    for(i in m) {
      var array = m[i];
      //do stuff
    }
    

    Keep in mind that for..in doesn’t guarantee to preserve the order of keys. However, all existing implementations of for..in do preserve the order.

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

Sidebar

Related Questions

I have been trying to assign value to an object array that is defined
I have the following loop that adds an object to an array if certain
I have a recursion function that parses an object/array with a global variable. If
Is there any way to obtain Object array of Java Bean fields? I have
I have an object that contains a array. On initialization of this object, the
I have simple AJAX function that uses jQuery to return an array of 300
I have an associative array (object) of the following construction var menu = new
I have an object that is inside of a form. His value is assigned
I have a nested JSON object that I need to loop through, and the
I have a map array with objects stuffed with variables looking like this: var

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.