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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:39:48+00:00 2026-06-02T04:39:48+00:00

I am almost there with this but cannot seem to get this functionality going

  • 0

I am almost there with this but cannot seem to get this functionality going as planned.

I have two arrays: keyArray and ValArray;

What I am trying to do is to have a function pass two arguments (keyArr,valArr). Within this function, a parent object is declared and a (for-loop) loops through the passed argument’s length (in this case “keyArr”) creates new objects according the length of the passed argument. And then, the newly created objects are assigned the keys and values.

The issue is that I am able to create the parent object”mObj”, and children Objects to “mObj”, but am only able to assgin keys and values to the first child object “obj0” not rest of the children objects correctly. At the end of the code, this is what I would like to get:

enter code heremObj.obj0.firstname = John;
mObj.obj0.lastname = superfly;
mObj.obj0.email = "john.superfly@yahoo.com";

mObj.obj1.firstname = John;
mObj.obj1.lastname = superfly;
mObj.obj1.email = "john.superfly@yahoo.com";

mObj.obj2.firstname = John;
mObj.obj2.lastname = superfly;
mObj.obj2.email = "john.superfly@yahoo.com";

This is my code:

 var keyArr = ["firstname","lastname","email"];
 var valArr = ["John","Superfly","jsuperfly@yahoo.com"];

 function test(keys,vals)  // FUNCTION TEST ACCEPTS TWO ARGS
 {
    var mObj = {}; // PARENT OBJECT
    var len = (keys.length); //ARGUMENT KEY'S LENGTH

    for(var i=0; i<len; i++) 
    { 
      mObj["obj" + i] = {};  //CHILDREN OBJECTS ARE CREATED TO PARENT "mObj" OBJECT
      mObj["obj" + i][keys[i]] = vals[i]; //KEYS AND VALUES ARE ASSIGNED HERE
    }

       alert(mObj.obj1.firstname); // CURRENTLY RETURNS "UNDEFINED"
 }

 test(keyArr,valArr);

Any insight into this would highly be appreciated.
Thank you.

  • 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-02T04:39:50+00:00Added an answer on June 2, 2026 at 4:39 am

    Seems like this is what you need. This code will create as many child objects as the length of keyArr and valArr arrays. Although no idea why you would need it.

    var keyArr = ["firstname", "lastname", "email"];
    var valArr = ["John", "Superfly", "jsuperfly@yahoo.com"];
    
    function test(keys, vals) {
        var mObj = {},
            i, j, len = keys.length;
    
        for (i = 0; i < len; i++) {
            mObj["obj" + i] = {};
            for (j = 0; j < len; j++) {
                mObj["obj" + i][keys[j]] = vals[j];
            }
        }
    
        alert(mObj.obj1.firstname);
    }
    
    console.log( test(keyArr, valArr) );​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is almost identical question , but I have no idea what to do
I'm pretty sure i'am almost there....but i cannot figure out how to iterate through
I have a website that makes heavy use of AJAX. There is an almost
While it seems that this has been asked many times before, but there's a
I'm almost there! I'm successfully pulling my MPEG-4 ES content from my SNC-RZ25N webcams
I'm almost there with something thanks to the help of stackoverflow users. I just
I'm trying to accomplish the following layout, http://www.rae-mx.com/test/css-layout.jpg and I'm almost there, except for
EDIT: There is the almost same question at Removing whitespace between HTML elements when
Almost anywhere I read about programming with CUDA there is a mention of the
Googling for the past week, but cannot finally decide which python web framework would

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.