Polia

https://www.w3schools.com/js/js_arrays.asp

https://www.w3schools.com/js/js_loop_for.asp

Jednoduche pole

<p id="demo"></p>
<script>
var cars = ["Saab", "Volvo", "BMW", 123456];
// cars.length ... je pocet prvkov v poli
document.getElementById("demo").innerHTML = cars.length;
var i;
var text = "";
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

Asociativne pole

<p id="demo"></p>
<script>
var text = "";
var cars = { "volvo":"vlasti Martin", "bmw":"vlastni Peter" };
text += cars["bmw"]+'<br>';
text += cars.volvo;
document.getElementById("demo").innerHTML = text;
</script>

Priklad 3

<p id="demo"></p>
<script>
var text = "";
var cars = {
"volvo":["vlasti Martin","farba je cervena"],
"bmw":{
"vlastnik":"vlastni Peter",
"farba":"farba je Modra",
"model":"model je M3"
}
};
text += cars.bmw.vlastnik+' - ';
text += cars.bmw.farba+' - ';
text += cars.bmw.model+'<br>';
text += cars.volvo;
document.getElementById("demo").innerHTML = text;
</script>

Priklad 4

<p id="demo"></p>
<script>
var text = "";
var cars = [
{'auto':'volvo', 'model':'xc90', 'cena':30000},
{'auto':'bmw', 'model':'M3', 'cena':40000},
{'auto':'skoda', 'model':'Oktavia', 'cena':20000}
];
var i;
for( i = 0; i < cars.length; i++ ){
text += 'auto '+cars[i].auto+'/'+cars[i].model+'/ = '+cars[i].cena+' EUR <br>';
}
document.getElementById("demo").innerHTML = text;
</script>

DU - Priklad c 4 spracovat v cykle forEach

teoria: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

priklad:

cars.forEach( car => {
  console.log("car:",car);
  text += car.auto +'<br>';
});

Tento web používa na analýzu návštevnosti súbory cookies.
Ak budete pokračovať bez vykonania zmien nastavení, predkpokladáme, že chcete z našej stránky dostávať všetky súbory cookie.