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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:42:22+00:00 2026-06-15T15:42:22+00:00

This is also referred to as "deep copying", which I’ve found some articles on.

  • 0

This is also referred to as "deep copying", which I’ve found some articles on. Closest seems to be this one but it’s for jQuery – I’m trying to do this without a library.

I’ve also seen, in two places, that it’s possible to do something like:

arr2 = JSON.decode(JSON.encode(arr1));

But that’s apparently inefficient. It’s also possible to loop and copy each value individually, and recurs through all the arrays. That seems tiring and inefficient as well.

So what’s the most efficient, non-library way to copy a JavaScript multi-dimensional array [[a],[b],[c]]? I am completely happy with a "non-IE" method if necessary.

Thanks!

  • 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-15T15:42:23+00:00Added an answer on June 15, 2026 at 3:42 pm

    Since it sounds like you’re dealing with an Array of Arrays to some unknown level of depth, but you only need to deal with them at one level deep at any given time, then it’s going to be simple and fast to use .slice().

    var newArray = [];
    
    for (var i = 0; i < currentArray.length; i++)
        newArray[i] = currentArray[i].slice();
    

    Or using .map() instead of the for loop:

    var newArray = currentArray.map(function(arr) {
        return arr.slice();
    });
    

    So this iterates the current Array, and builds a new Array of shallow copies of the nested Arrays. Then when you go to the next level of depth, you’d do the same thing.

    Of course if there’s a mixture of Arrays and other data, you’ll want to test what it is before you slice.

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

Sidebar

Related Questions

This expression seems to work: gci . | % { gc $_} This also
Xcode optimizes png images so they are loaded faster, but is this also recommended
I realize this is more of a hardware question, but this is also very
Code-wise this seems so simple, but I still get stuck on a #VALUE. I
i am working on a project which requires some image processing, i also asked
Can i show a Tool tip like this: Also, I want to show this
In C specifically (i suppose this also applies to C++), what is the difference
Symbian has a stack limit of 8kB. Does this also apply to the function
MOSS suports installing document converters on the server, is this also supported in WSS?
Hi: I want to redirect stdout to a NSTextView. Could this also work with

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.