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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:58:21+00:00 2026-05-29T16:58:21+00:00

I have two arrays: var columns = ["Date", "Number", "Size", "Location", "Age"]; var rows

  • 0

I have two arrays:

var columns = ["Date", "Number", "Size", "Location", "Age"];

var rows = [
  ["2001", "5", "Big", "Sydney", "25"],
  ["2005", "2", "Med", "Melbourne", "50"],
  ["2012", "20", "Huge", "Brisbane", "80"]
];

I’m trying to combine them into a javascript object for each item in the rows array. After that, I want to push each object into a new array.

Like:

var newarray = [];

//'thing' should be the same structure for each row item
var thing = {
  "Date": "2001",
  "Number": "5",
  "Size": "Big",
  "Location": "Sydney",
  "Age": "25"
}

newarray.push(thing);

I can do this when I know the names of the columns, but I need to be able to store the data in this way when the column name is unknown – i.e. based on the indexes of the columns array.

I tried it like this before:

for (var y = 0; y < rows.length; y++) {

  for (var i = 0; i < columns.length; i++) {
    thing[columns[i]] = rows[i][i];
  }
  newarray.push(thing)
}

The code above only stored the first item again and again (according to rows.length).

I don’t understand how to combine the column names with the rows to create an array of objects. The fact that ‘rows’ contains arrays is especially confusing..

  • 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-29T16:58:23+00:00Added an answer on May 29, 2026 at 4:58 pm

    Create a new thing object at the beginning of each iteration of the outer loop. If you don’t do so, each time you say thing[columns[i]] you’ll be overwriting the properties of the same object, and when you push it into newarray what you’ll end up with is an array full of references to the same object. Also, inside the loop be sure you get the indexes right (you’ve currently got [i][i] instead of [y][i]):

    var newarray = [],
        thing;
    
    for(var y = 0; y < rows.length; y++){
        thing = {};
        for(var i = 0; i < columns.length; i++){
            thing[columns[i]] = rows[y][i];
        }
        newarray.push(thing)
    }
    

    “The fact that ‘rows’ contains arrays is especially confusing..”

    For your sample data rows.length will be 3, and rows[0] is the array:

    ["2001", "5", "Big", "Sydney", "25"]
    

    rows[0][3] is “Sydney”.

    Using that as an example you should be able to see what rows[y][i] will give you for each value of y and i…

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

Sidebar

Related Questions

Let's say I have these two arrays: var array1 = new[] {"A", "B", "C"};
I have two JavaScript arrays: var array1 = [Vijendra,Singh]; var array2 = [Singh, Shakya];
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two arrays: var arr1 = [1, 2, 3], arr2 = [2, 5,
I have two arrays of objects: var a = [ {'id': 20}, {'id': 15},
I have two arrays. For example: var bool = [true, false, true]; var vals
I have two arrays: var content = { girls: [Maria, Angela, Bianca], digits: [21.143.191.2,
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays. One contains id=>count and the other contains id=>name . I'm
I have two arrays containing the same elements, but in different orders, and I

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.