indexOf()
searches the array
to see if the argument passed to the function is found in the array.
If the argument is contained in the array, the function returns the index position of the argument.
If the argument is not found in the array, the function returns -1.
Here is an example:
var names = ["A", "C", "R", "D", "J"]; var name = "C"; var position = names.indexOf(name); if (position >= 0) { console.log("Found " + name + " at position " + position); } else { console.log(name + " not found in array."); }
The code above assigns the found index to the variable named position. Then it uses the if statement to check the value of position. If its value is greater than 0 we can say the element is contained in the array. Otherwise the value is not in the array.
If you have multiple occurrences of the same data in an array, the indexOf()
function
will always return the position of the first occurrence.
lastIndexOf()
will return the position of the
last occurrence of the argument in the array, or -1
if the argument isn't found.
Here is an example:
var names = ["CSS", "Mike", "HTML", "SQL", "CSS", "Mike", "Java"]; var name = "Mike"; var firstPos = names.indexOf(name); console.log("First found " + name + " at position " + firstPos); var lastPos = names.lastIndexOf(name); console.log("Last found " + name + " at position " + lastPos);
The names
has two Mikes.
join()
and toString()
convert array to String.
Both functions return a string containing the elements of the array delimited by commas.
Here are some examples:
var names = ["CSS", "Mike", "HTML", "SQL", "CSS", "Mike", "Java"]; var namestr = names.join(); console.log(namestr); namestr = names.toString(); console.log(namestr);
When you call the console.log() function with an array name, it automatically calls the toString() function for that array.
console.log(names);
There are two accessor functions that create new arrays from existing arrays:
concat()
and splice()
.
The concat()
function puts together two or more arrays to create a new array.
concat()
function is called from an existing array,
and its argument is another existing array.
The argument is concatenated to the end of
the array calling concat()
.
The following program demonstrates how concat()
works:
var firstArray = ["A", "Z", "T", "D", "J"]; var secondArray = ["CSS", "C", "B"]; var thirdArray = cis.concat(dmp); console.log(thirdArray); thirdArray = dmp.concat(firstArray); console.log(thirdArray);
The splice()
function creates a new array from a subset of an existing array.
The splice()
function can also create a new array from an existing array.
The arguments to the function are the starting position for taking the splice and the number of elements to take from the existing array.
Here is how the method works:
var thirdArray = ["M","C","T","A","D","E","J"]; var secondArray = thirdArray.splice(3,3); var firstArray = thirdArray; console.log(secondArray); console.log(firstArray);
There are other uses for splice()
, such as modifying an array by adding
and/or removing elements.