Sunday, April 25, 2010

copy a multidimensional array in js.

I'm working on ticket #587, for processing.js.

What I have to do is convert a custom ArrayList function to create a multidimensional array based on parameters passed in. For example:

var array = new ArrayList(4, 5, 6);

would create a 3D array, similar to:

int[][][] array = new int[4][5][6]

This is what I've put together:

p.ArrayList = function() {
var array = [],
tempArray = [];

for (var i = 0, aLength = arguments[arguments.length-1]; i < aLength; i++){
array[i] = 0;
}

for (var i = arguments.length-2; i >= 0; i--){
tempArray = array.slice();
array = [];
for (var j = 0, aLength = arguments[i]; j < aLength; j++){
array[j] = tempArray.slice();
}
}
return array;
};

It starts at the back of the arguments, and copies the array recursively until it's done arguments[0], then returns the array.

In JavaScript arrays are copied by reference, and not by value, but if you use array.slice() it creates a copy of the array's values, but not a copy of the array inside the array, those are still passed by reference. In other words, it only works for single, and 2D arrays, anything larger and you start getting arrays containing references to the same data, which is not good.

I'm going to have to find a better way to do this, it must be recursive, and must be based on the number of arguments.

26 comments:

  1. Great content thanks for sharing this informative blog which provided me technical information keep posting.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  2. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    Data Science Course in Indira nagar
    Data Science Course in btm layout
    Python course in Kalyan nagar
    Data Science course in Indira nagar
    Data Science Course in Marathahalli
    Data Science Course in BTM Layout
    Data science course in bangalore

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Great web site you have got here.. It’s hard to find quality writing like yours these days. I really appreciate individuals like you! Take care!!
    UI Development Training In Marathahalli
    Selenium Training In Marathahalli

    ReplyDelete
  5. Attend The Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Bangalore.

    ReplyDelete
  6. I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it..360DigiTMG big data in malaysia
    360DigiTMG artificial intelligence in malaysia
    360DigiTMG machine learning course
    360DigiTMG

    ReplyDelete
  7. nice post
    aws training in hyderabad
    https://360digitmg.com/amazon-web-services-aws-training-in-hyderabad
    AWS training will give the students obtain expertise in the theories of AMI Creation, EBS Persistent Storage, Amazon Storage Services S3, Route 53, AWS EC2 and AWS S3 Instances & further high-level concepts.

    ReplyDelete
  8. It is the first time I have been here. I suppose I'm not just one with all the enjoyment right here from the hundreds of feedback on your posts.financial analytics course malaysia

    ReplyDelete
  9. I am dazzled by the data that you have on this blog. It shows how well you comprehend this subject.artificial intelligence course in delhi

    ReplyDelete
  10. Next time I read a blog, Hopefully it doesn't fail me as much as this particular one. After all, Yes, it was my choice to read, but I really believed you would probably have something useful to talk about. All I hear is a bunch of complaining about something you can fix if you were not too busy looking for attention.
    tech gadget

    ReplyDelete
  11. This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it,
    cyber security training malaysia

    ReplyDelete
  12. Rekordbox DJ Crack as the name suggests, is a great DJ software. Useful for composing great music. This software provides a platform to. Rekordbox Dj Crack

    ReplyDelete