Array Metodi e proprieta 2




'Aggiunta di elementi di matrice

'Il modo piu' semplice per aggiungere un nuovo elemento a un array e' utilizzare il push()metodo:


'Esempio

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

'fruits.push("Lemon"); // adds a new element (Lemon) to fruits


https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_add_push

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>

<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("Lemon");
document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>


'Il nuovo elemento puo' anche essere aggiunto a un array usando la lengthproprieta':


'Esempio

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

'fruits[fruits.length] = "Lemon"; // adds a new element (Lemon) to fruits


https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_add

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>

<p>The length property provides an easy way to append new elements to an array without using the push() method.</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[fruits.length] = "Lemon";
document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>



'Array associativi

'Molti linguaggi di programmazione supportano matrici con indici nominati.

'Gli array con indici nominati sono chiamati array associativi (o hash).

'JavaScript non supporta matrici con indici nominati.

'In JavaScript, gli array utilizzano sempre indici numerati .


'Esempio

'var person = [];

'person[0] = "John";

'person[1] = "Doe";

'person[2] = 46;

'var x = person.length; // person.length will return 3

'var y = person[0]; // person[0] will return "John"


https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_associative_1

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>

<p id="demo"></p>

<script>
var person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
document.getElementById("demo").innerHTML =
person[0] + " " + person.length;
</script>

</body>
</html>

' Evita la nuova matrice ()

'Non e' necessario utilizzare l' newArray () del costruttore di array integrato di JavaScript .

'Usa []invece.

'Queste due diverse istruzioni creano entrambi un nuovo array vuoto chiamato punti:

'var points = new Array(); // Bad

'var points = []; // Good


https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_literal

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>

<p>Avoid using new Array(). Use [] instead.</p>

<p id="demo"></p>

<script>
//var points = new Array(40, 100, 1, 5, 25, 10);
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points[0];
</script>

</body>
</html>










( arraymetodieproprieta2.html )- by Paolo Puglisi - Modifica del 17/12/2023