473,581 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array.slice() not creating an independent copy.

Alright, am I missing something?

I create a 2D array like so:

var blah = [];
blah[0] = ['one', 'two', 'three'];
blah[1] = ['four', 'five', 'six'];

Then I *attempt* to create an independent copy based on all of the pages I have read that
said it was as so:

var copy_of_blah = blah.slice(); // does not create independent copy
var copy_of_blah = blah.slice(0); // nor does this

I tested it by immediately changing either:

blah[0][0] = ''; // "one" is now ''

*or*

copy_of_blah[0][0] = ''; // "one", is again, ''

And of course both reflect changes upon the other.

Is it possible to create an independent copy of an array without having to write a
function that dumps the contents into a new array?

-Lost
Mar 11 '07 #1
2 3752
On Mar 11, 4:59 pm, "-Lost" <missed-s...@comcast.ne twrote:
[...]
Is it possible to create an independent copy of an array without having to write a
function that dumps the contents into a new array?
Only if the elements of the array are primitives, where you can use
concat. If you have an array of arrays and use slice, your 'copy'
consists of an array of references to the arrays in the original
array.
--
Rob

Mar 11 '07 #2
"RobG" <rg***@iinet.ne t.auwrote in message
news:11******** **************@ 8g2000cwh.googl egroups.com...
On Mar 11, 4:59 pm, "-Lost" <missed-s...@comcast.ne twrote:
[...]
>Is it possible to create an independent copy of an array without having to write a
function that dumps the contents into a new array?

Only if the elements of the array are primitives, where you can use
concat. If you have an array of arrays and use slice, your 'copy'
consists of an array of references to the arrays in the original
array.
Primitives, as in no Objects.

Thanks, RobG.

-Lost
Mar 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9869
by: kr | last post by:
Hi there, Is it possible to slice an array from a particular element to the end of it without using $#<array name>? My goal is the get only the IP addresses from the output of gethostbyname(). Thanks
11
4442
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
4
2026
by: Christopher Benson-Manica | last post by:
Is array.slice() guaranteed to return a zero-length array if the first argument is greater than the length of the array? Or is it allowed to throw an exception? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
1
8672
by: Antony Sequeira | last post by:
I wanted to make a type safe array slice method (at least for single dimension arrays) Here is what I have public static <T> T arraySlice(T src, int offset, int length) { int dim = new int ; dim = length; T dst; dst = (T ) java.lang.reflect.Array.newInstance(src.getClass().getComponentType() ,
7
1921
by: RubyRedRick | last post by:
I bought Crockford's "JavaScript: The Good Parts" yesterday to help build my JavaScript foo. On page 44, he gives an implementation of the curry function: Function.method('curry', function() { var slice = Array.prototype.slice, args = slice.apply(arguments), that = this;
12
2046
by: lorlarz | last post by:
In the code sample below, how are arguments a legitimate argument to Array.slice? Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift(); return function(){ return fn.apply(object, args.concat(Array.prototype.slice.call(arguments)));
0
7868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8175
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5674
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5364
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.