Você está na página 1de 5

JavaScript Arrays

Create an array
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars[0];
</script>
</body>
</html>
Join two arrays - concat()
<!DOCTYPE html>
<html>
<body>
<p>Click "Try it" to display all my children:</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var myGirls = ["Cecilie", "Lone"];
var myBoys = ["Emil", "Tobias", "Linus"];
var myChildren = myGirls.concat(myBoys);
document.getElementById("demo").innerHTML = myChildren;
}
</script>
</body>
</html>
Join three arrays - concat()
<!DOCTYPE html>
<html>
<body>
<p>Click the button to join three arrays.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var arr1 = ["Cecilie", "Lone"];
var arr2 = ["Emil", "Tobias", "Linus"];
var arr3 = ["Robin", "Morgan"];
document.getElementById("demo").innerHTML =
arr1.concat(arr2, arr3);
}
</script>
</body>
</html>
Join all elements of an array into a string - join()
<!DOCTYPE html>
<html>
<body>
<p>the join() method joins array elements into a string.</p>
<p id="demo"></p>
<script>

var fruits = ["Banana", "Orange", "Apple", "Mango"];


document.getElementById("demo").innerHTML = fruits.join(" * ");
</script>
</body>
</html>
Remove the last element of an array - pop()
<!DOCTYPE html>
<html>
<body>
<p>The pop method removes the last element from an array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.pop()
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
Add new elements to the end of an array - push()
<!DOCTYPE html>
<html>
<body>
<p>The push method appends a new element to an array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.push("Kiwi")
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
Reverse the order of the elements in an array - reverse()
<!DOCTYPE html>
<html>
<body>
<p>The sort() method sorts an array alphabetically.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.sort();
fruits.reverse();
document.getElementById("demo").innerHTML = fruits;

}
</script>
</body>
</html>
Remove the first element of an array - shift()
<!DOCTYPE html>
<html>
<body>
<p>The shift method removes (shifts) the first element of an array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.shift();
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
Select elements from an array - slice()
<!DOCTYPE html>
<html>
<body>
<p>The slice() method slices elements from an array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);
document.getElementById("demo").innerHTML = citrus;
}
</script>
</body>
</html>
Sort an array (alphabetically and ascending) - sort()
<!DOCTYPE html>
<html>
<body>
<p>The sort() method sorts an array alphabetically.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.sort();
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>

</html>
Sort numbers (numerically and ascending) - sort()
<!DOCTYPE html>
<html>
<body>
<p>Click the button to sort the array in ascending order.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function myFunction() {
points.sort(function(a, b){return a-b});
document.getElementById("demo").innerHTML = points;
}
</script>
</body>
</html>
Sort numbers (numerically and descending) - sort()
<!DOCTYPE html>
<html>
<body>
<p>Click the button to sort the array in descending order.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function myFunction() {
points.sort(function(a, b){return b-a});
document.getElementById("demo").innerHTML = points;
}
</script>
</body>
</html>
Add an element to position 2 in an array - splice()
<!DOCTYPE html>
<html>
<body>
<p>The splice() method adds new elements to an array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.splice(2, 0, "Lemon", "Kiwi");
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
Convert an array to a string - toString()
<!DOCTYPE html>

<html>
<body>
<p>The toString() method returns an array as a comma separated string.</p>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
</script>
</body>
</html>
Add new elements to the beginning of an array - unshift()
<!DOCTYPE html>
<html>
<body>
<p>The unshift method adds new elements to the beginning of an array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.unshift("Lemon");
document.getElementById("demo").innerHTML = fruits;
}
</script>
<p><b>Note:</b> The unshift() method does not work properly in Internet Explorer 8 and earlier, the values will be
inserted, but the return value will be <em>undefined</em>.</p>
</body>
</html>

Você também pode gostar