473,385 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3745
On Mar 11, 4:59 pm, "-Lost" <missed-s...@comcast.netwrote:
[...]
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.net.auwrote in message
news:11**********************@8g2000cwh.googlegrou ps.com...
On Mar 11, 4:59 pm, "-Lost" <missed-s...@comcast.netwrote:
[...]
>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
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(). ...
11
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...
4
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 |...
1
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...
7
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() {...
12
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 =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.