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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:44:46+00:00 2026-06-15T09:44:46+00:00

Here is my code: I have two arrays[lab ,val] whose values are to be

  • 0

Here is my code:
I have two arrays[lab ,val] whose values are to be dynamicaly passed to object.
k is array to hold objects

var lab = ["1","2","3",4];
var val = ["sa","ma","pa","da"];
var k = [];
for(var i=0; i<4; i++)  {
   data[i] = {};             
   data[i].label = lab[i];
   data[i].value = val[i]; 
   k.push(data[i]) ;  
}

document.write(k.data[0]);  
document.write(" " + k.data[0] + "<br />");
  • 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-15T09:44:48+00:00Added an answer on June 15, 2026 at 9:44 am

    Since you are already pushing the objects into an array, do it like this –

    var lab = ["1","2","3",4];
    var val = ["sa","ma","pa","da"];
    var k = [];
    var obj;    // it will hold temporary object references
    
    for(var i=0; i<4; i++)  {
       obj = {};             
       obj.label = lab[i];
       obj.value = val[i]; 
       k.push(obj) ;  
    }
    
    document.write(k[0].label);    // index k, rather than the value it contains 
    document.write(" " + k[0].value + "<br />");
    

    Your code is not working because you are indexing the values that k stores, and your data variable isn’t declared anywhere.

    You can even shorten the for loop by directly pushing the objects into the array, which is a better way –

    for(var i=0; i<4; i++)  {
       k.push({
           label: lab[i],
           value: val[i]
       }) ;  
    }
    

    In this way you won’t even need the temporary object.

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

Sidebar

Related Questions

TEST SITE Look here to see code in action I have two arrays which
I have this code here that has two arrays. It sorts arr[] , so
i have two dropdown list. first drop down:1 enter code here <form:select path=custName id=custName>
Here is my code, I have two checkboxes and want to keep one disabled
here is what my code looks like i have two forms: class Form_1 extends
Ok so here is a question. I have two projects in code igniter, say
What is the issue with this code? Here we have two files: classA.h and
I have a class with two constructors (C#). Here is the code snippet: public
I have two arrays a={a,b,c,d} b={1,2,3,4} now I want to create a 3rd array
I have two arrays values and keys both of the same length. I want

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.