PDA

View Full Version : What's the difference between splice and '='


thepeskykid
06-19-2003, 10:14 AM
Hello

What is the difference between...

tempArrayI.splice(1,1,[description]);

and...

tempArray[1] = description;

I have read up on splice and I understand it is deleting 1 item at position 1 in the array tempArrayI, then replacing it with the value of 'description'.

The '=' approach appears to be doing the same thing (both appear to do the same thing in my program) but I suspect the '=' approach is doing something subtly different, like maybe acting as a reference or something.

Could someone please clarify this for me?

Thank you

:)

beetle
06-19-2003, 03:49 PM
Referencing in javascript is dependant upon the data type being assigned. Primitives are stored, objects are referenced unless they are being instantiated.

var myVar = 1; // stored

var myVar = document.body; // reference

var myVar = new Object(); // stored
var myVar2 = myVar; // reference

Now, when you assign values to arrays, you need to specify which index in the array will receive that value.

tempArray[i] = someValue;

Just assigns the given value to the given index - nothing more complicated than that.

Now, about Array.splice(). The splice method takes two required arguments, start and deleteCount, and n optional arguments. splice is a mutating method - meaning it modifes the object on it's own without requiring help from you. It removes deleteCount indexes from the array, starting at index start. After doing so, it will incrementally add n to the array, inserting them all into the gap created by the splice . Lastly, it return all removed indexes as a new array object.

So, let's look in detail as your example. Let's take the following array

var colors = ['red','orange','yellow'];

We can say

colors[1] = 'blue';

And now our array looks like ['red','blue','yellow']

By doing the same thing with splice

color.splice(1,1.'blue');

Here's what a "trace" of this opertaion would look like

// Remove index 1
['red','yellow']

// Add new value at splice location
['red','blue','yellow'];

// return removed values
['orange'] ['red','blue','yellow'];

So - both accomplish the same modification to the array - but splice additionally returns the old value(s).

Unless you need that old value returned, or wish to make several index replacements with one operation, you should not use splice(). It's more costly to perform, and is only supported by newer versions of JS (particularly IE, which requires IE5.5 to support splice())

thepeskykid
06-19-2003, 07:14 PM
thank you

checked out your website - very impressed

:)

beetle
06-19-2003, 07:22 PM
Why thank you :D

thepeskykid
06-19-2003, 07:41 PM
I have another question you may be able to clarify for me beetle...

The project I am working on (just a self-generated one to learn JS) requires data entered into form fields, then stored in arrays, then (in some cases) processed mathematically, then either rendered out as html 'reports' or sometimes placed back into form fields.

I think the following is how this is done with JS...

form field input data exists as strings

this data (when passed into arrays) remains strings

any numerical array stored data that needs math processing needs to be turned from strings into numbers (integers/float) before such processing

stored array data must be converted back to strings before html rendering

am I on the right track here - or hopelessly of the mark

:)