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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:40:01+00:00 2026-06-17T11:40:01+00:00

I try to create a new multidimensional array from another multidimensional array. The first

  • 0

I try to create a new multidimensional array from another multidimensional array. The first array looks like the following:

array[0][0] = foo
array[0][1] = foo2
...and so on
array[1][0] = 123
array[1][1] = 234
...and so on
array[2][0] = blue
array[2][1] = red
...and so on
array[3][0] = fish
array[3][1] = bird
...and so on

…then I have an array called ‘results’ that contains of numbers (e.g. 1, 20 , 23). What I want to do is to create a new multidimensional array, just like the first, one but only with the elements from the first one that’s to be found as numbers in the array ‘results’.

Below is my try so far that doesn’t work. When I run the code I get the following error message:

Uncaught TypeError: Cannot set property '0' of undefined) <- line 5

What am I doing wrong?

1: var newArray = [];
2:
3: for (var i = 0; i < results.length; i++) {
4:    var index = results[i]
5:    newArray[0][i] = array[0][index];
6:    newArray[1][i] = array[1][index];
7:    newArray[2][i] = array[2][index];
8:    newArray[3][i] = array[3][index];
9: }
  • 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-17T11:40:02+00:00Added an answer on June 17, 2026 at 11:40 am

    you always need to initialize the array:

    newArray[0] = [];
    

    like in this example:

    var newArray = [];
    // newArray[12345] is undefined
    newArray[12345][2] = 3; // error!
    

    newArray[12345] is undefined, and you want to treat undefined element as array. so before you can do that you need to initalize the subarray:

    var newArray = [];
    newArray[12345] = []; // now "newArray[12345]" is new subarray
    newArray[12345][2] = 3;
    

    Edit:

    var newArray = [];
    
    for (var i = 0; i <= 3; i++) {
        newArray[i] = []; 
        //newArray[i] = new Array(); - same thing
    }
    
    for (var i = 0; i < results.length; i++) {
        var index = results[i]
        newArray[0][i] = array[0][index];
        newArray[1][i] = array[1][index];
        newArray[2][i] = array[2][index];
        newArray[3][i] = array[3][index];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use a from a multi-dimensional array that I create in another
I try create new function in Excel, witch will counting given values (something like
Dears, in SSIS 2008 when I try to create new package I get following
I'd like to create a dynamic Multidimensional ArrayList that reads from a text file
I try to create a new file inside a JSP and try to save
I'm try to create a new list object. I set the new object to
Whenever I try to edit existing or create new distribution provisioning profile on iOS
I want to try create something like Zend's Server Pagecache. What I want to
I try to create styles with first-child and last-child items but I encountered a
I know that when I try to create new MessageQueue , system throws InvalidOperationException

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.