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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:55:21+00:00 2026-06-05T19:55:21+00:00

Is there a way in JavaScript to select a element of a multidimential array.

  • 0

Is there a way in JavaScript to select a element of a multidimential array. Where the depth/rank/dimensionality is variable and the keys are given by a array of indices. Such that i don’t have handle every possible dimentional depth separately. concretely speaking i want do get rid of switch cases like here:

/**
 * set tensor value by index
 * @type {array} indices [ index1, index2, index3 ] -> length == rank.
 * @type {string} value.
 */
tensor.prototype.setValueByIndex = function( indices, value ) {
    var rank = indices.length;

    switch(rank) {
        case 0:
            this.values[0] = value;
        break;
        case 1:
            this.values[indices[0]] = value;
        break;
        case 2:
            this.values[indices[0]][indices[1]] = value;
        break;
        case 3:
            this.values[indices[0]][indices[1]][indices[2]] = value;
        break;
    }
}

Where this.values is a multidimensional array.

such that i get something that looks more like this:

/**
 * set tensor value by index
 * @type {array} indices, [ index1, index2, index3 ] -> length == rank
 * @type {string} value
 */
tensor.prototype.setValueByIndex = function( indices, value ) {
    var rank = indices.length;

    this.values[ indices ] = value;
}

Thank you in advance!

  • 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-05T19:55:23+00:00Added an answer on June 5, 2026 at 7:55 pm
    tensor.prototype.setValueByIndex = function( indices, value ) {
        var array = this.values;
        for (var i = 0; i < indices.length - 1; ++i) {
            array = array[indices[i]];
        }
        array[indices[i]] = value;
    }
    

    This uses array to point to the nested array we are currently at and reads through the indicies for find the next array value from the current array. Once we reach the last index in the indices list, we have found the array where we want to deposit the value. The final index is the slot in that final array where we deposit the value.

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

Sidebar

Related Questions

I there ANY way in javascript that we could trigger a select element (dropdown
Is there a way to select a element by index with Javascript or jQuery?
Is there a quick way in javascript to select the element that called the
Is there a way to select all CSS rules of an element with JavaScript?
Is there a way in JavaScript to compare values from one array and see
Is there a way to select the top most DOM element at a certain
Is there any way to convert a string to a variable in Javascript? Right
Is there a way to change the options array of an html select list
There is some strangeness while I create in JavaScript Select element: var items =
Is there a way to make a HTML select element call a function each

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.