Javascript - Array

¡Supera tus tareas y exámenes ahora con Quizwiz!

toString();

converts all elements of an array to a string and returns a comma separated list of them

array.concat();

creates and returns a new array that contains the elements of the original array on which concat was invoked, followed by each of the arguments. Ex: var a = [9,10,11] a.concat(12,13,14} returns [9, 10, 11, 12, 13, 14]

array.shift();

deletes the first element of an array

array.pop();

deletes the last element of an array

How would you test for any inherited properties an array would have from its prototype and filter them out?

if (!a.hasOwnProperty(i)) continue; Which skips inherited properties

What code in a for loop would skip null, undefined, or nonexistent elements?

if (!a[i]) continue;

What code in a for loop would skip undefined, or nonexistent elements?

if (a[i] === undefined) continue;

indexOf() and lastIndexOf()

indexOf() method searches an array for an element with a specified value and returns the index of the first such element found, or -1 if it can't be found. lastIndexOf() returns the last index that meets that criteria.

What code in a for loop would skip nonexistent elements?

(!(i in a)) continue;

What is the value of the length property?

i + 1 because the first index number = 0.

How do you access an array by an offset?

var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]; console.log(languages[2])

How are arrays different from objects?

1. The length property is automatically updated as new elements are added to the list 2. Setting length to a smaller value truncates the array 3. arrays inherit methods from array.prototype 4. arrays have the class attribute "Array"

Define array.join()

A method that converts all elements of an array to a string and concatenates them returning the resulting string

What is an array?

An array is an ordered collection of values.

What is a multidimensional array?

An array that has one or more arrays nested inside of it

What is a jagged array?

An array that is composed of two or more arrays that do not have the same number of values in their respective arrays

What is a heterogeneous array?

An array that is comprised of different types of values: strings, numbers, and booleans

What is a element?

An element is a value in an array.

What is the length property for sparse or nonsparse arrays? How does the index number related to the length?

For non-sparse arrays, the length equals the number of elements in the array. For sparse arrays, the length is larger than the index of the elements because of empty indexes.

How is reduceRight() different from array.reduce()?

It begins at the array from the highest index and works backwards.

What does it mean that JS arrays are sparse?

It means that the elements do not have contiguous indexes from zero, but that there may be gaps.

What does it mean that JS arrays are dynamic?

It means they grow and shrink as needed; there is no need to declare a fixed size.

What happens when you call an array index that does not exist?

It returns undefined because it is a type of object property

Are arrays typed or untyped?

JS arrays are untyped, meaning any type of data can be inserted as a value.

How can you make a value of an array spare?

Use the delete operator to delete a pre-existing value or specify a value at an index using a[995] = 100; So the value of index number 995 of a array is now 100.

What happens if you delete a value from an array index?

That index becomes sparse.

array.map();

The map() method passes each element of the array on which it is invoked to the function you specify, and returns an array containing the value by that function

In the construct var a = new array(45); what does the 45 stand for?

The number of indexes the array should create. Additional arguments would specify the values of the array.

What is an index?

The ordered position of the elements in the array.

What happens if you set a new length to an array that is smaller than the current length of it?

The values of index numbers larger than the new length of the array will be deleted.

Are all indexes property names?

Yes, but not all property names are indexes. Only numbers between 0 - 2^32-2.

How do you sort an array by a means other than alphabetical?

You must pass a comparison function . This argument defines which of two arguments should appear first. Ex: a.sort(function(a,b) { return a-b; }); to sort from greatest to least.

If you need to terminate the loop early when using .forEach() method, how does it work?

You must throw an exception and place the call to forEach() within a try block.

Other than assigning values to new indexes, how can elements of an array be created?

array.push(); ---> adds one or more elements to the end of the array array.unshift() ---> pushes an array value to the beginning of an array and shifting all other values to a larger index

array.every();

every() method apply a predicate function you specify to the elements of the array, and then return true and false. It will only return true if the predicate is true for ALL elements of the array.

array.splice();

general purpose method for inserting or removing elements from an array. It modifies the array on which it is invoked. First two arguments specify which values should be deleted, and any additional arguments specify values to be added. //removes 2 elements from index 0, and inserts "parrot", "anemone" and "blue" removed = myFish.splice(0, 2, "parrot", "anemone", "blue"); //myFish is ["parrot", "anemone", "blue", "trumpet", "surgeon"] //removed is ["angel", "clown"] first number is index number (where to start insertion/deletion), second number is # to delete, and third are the items to add.

array.forEach();

iterates through an array and invokes a function you specify for each element

array.slice();

returns a slice of a specific subarray. Its first argument specifies the beginning, the second the end. If no end is specified, it returns all elements from the specified start to the end of the array. A negative value specifies the index value relative to the last value (i.e. "-3" equals three from the last value)

Array.isArray();

returns true or false depending on if an object is an array or not

array.reverse();

reverses the order of elements in an array and returns the reversed array.

array.some();

some(); method will return true if there exists at least one element in the array for which the predicate returns true.

array.sort();

sorts elements of an array in place and returns the sorted array. When sort() is called with no arguments, it returns indexes in alphabetical order. Undefined elements sort to the back of the array.

array.filter();

the array.filter() returns an array containing a subset of the elements of the array on which it was invoked. The function must be able to return a true or false on each element. If it is true, it will pass the element to a subset and add it to the new array.

array.reduce():

the reduce() method combines the elements of an array using the function you specify to produce a single value. The first part performs the reduction operation and the optional second argument tells the function the initial value to begin with.

What is an array constructor? How could you add to array constructors?

var a = new array(); Constructor array with no arguments. var family = new Array(); family[0] = new Person("alice", 40); family[1] = new Person("bob", 42);

How could you add an object to an array?

var cat = {name: "Chunkie", Age: 18} var myArray = [2, false, "apple pie", [cat]] console.log(myArray);

What is the construct for an array literal?

var empty = []; An empty array.


Conjuntos de estudio relacionados

Which of the following best describes the field of physical geography?

View Set

NURS-316 Final: Lecture 30 (Ethical and Legal Issues)

View Set

Mental Health Chapters: 5, 7, 8, 9, 10, 11, 12, 13, 14, 15

View Set

Unit 8- Rise of Labor Unions Review

View Set

The Theory of Plate Tectonic JPJ

View Set

Criminal Investigation CH. 2 CTC-Killeen

View Set

Chapter 3 : Life insurance policy riders, provisions, options, and exclusions

View Set