Skip to main content

News

Topic: Arrays and Methods (Read 1451 times) previous topic - next topic

Arrays and Methods
  Mozilla Tutorial

Looking over the descriptions for what you can do with arrays.
About the only one I understand is array.length which obtains the number value of how many elements are attached in the array.

The ones I increasingly come into contact with and need help comprehending are:
Quote

1.) array.push - Adds one or more elements to the end of an array and returns the new length of the array.
2.) array.shift - Removes the first element from an array and returns that element.
3.) array.splice - Adds and/or removes elements from an array.
4.) array.slice - Extracts a section of an array and returns a new array.
5.) array.toString - Returns a string representing the array and its elements. Overrides the Object.prototype.toString method.


Currently I'm having to mess with array.push() and am wondering what it does.
I know it relies on array.length, from what the Mozilla reference states,
but what exactly is it doing to the number of elements?
Is it calling one of the elements by singling out by using array.length?
That's what I'm assuming.

And I know array.splice is adding a new element to the array, or removing one that was specified.
But in the examples on the Mozilla site, they're adding these weird number values.
Quote
myFish.splice(2, 0, "drum");

What is the (2, 0) part for?
  • Last Edit: July 10, 2013, 04:39:24 pm by Vakinox

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Arrays and Methods
Reply #1
Quote
Currently I'm having to mess with array.push() and am wondering what it does.

Really simple: it pushes a new item onto the end of the array.

push
Imagine you have this array:
Code: (javascript) [Select]
var alphabet = ["a", "b"];

That means it has these values:
Code: (javascript) [Select]
alphabet[0] == "a"
alphabet[1] == "b"
alphabet.length == 2


Now we push an item on top of it:
Code: (javascript) [Select]
alphabet.push("c")

And now the array consists of this:
Code: (javascript) [Select]
alphabet[0] == "a"
alphabet[1] == "b"
alphabet[2] == "c"
alphabet.length == 3



splice
As for array.splice, the 2 defines that your starting point is at position 2 of the array. The 0 indicates how many elements to remove from that point (in this case none). The "drum" inserts that at position 2 and pushes all elements in the array that were 2 or higher up. Example with the alphabet array:
Code: (javascript) [Select]
alphabet.splice(2, 0, "d");

So now you have these values in the array:
Code: (javascript) [Select]
alphabet[0] == "a"
alphabet[1] == "b"
alphabet[2] == "d"
alphabet[3] == "c"
alphabet.length == 4


And removing 2 elements from point 0 onward:
Code: (javascript) [Select]
alphabet.splice(0, 2);

You now have these values:
Code: (javascript) [Select]
alphabet[0] == "d"
alphabet[1] == "c"
alphabet.length == 2

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Arrays and Methods
Reply #2
Array.push is really useful as it easily lets you add new elements to an array. Practical example:

Code: (javascript) [Select]
var characters = [];
characters.push(new Player("hero"));
characters.push(new Player("sidekick"));