[JavaScript] Arrays definieren

Eindimensionale Arrays

// https://www.w3schools.com/jsref/jsref_obj_array.asp
// https://www.w3schools.com/js/js_array_methods.asp

// leeres array
let arr_e = [];
console.log(arr_e.length);

// String array
let arr_s = ["a", "b", "c"];
for (let s in arr_s)
{
    console.log(arr_s[s]);
};

// Float array
let arr_f = [1.1, 2.1, 0.3];
for (let f in arr_f)
{
  console.log(arr_f[f]);
}

// Int array
let arr_i = [1, 2, 3];
for (let i in arr_i)
{
    console.log(arr_i[i]);
}

// new Array()
let arr_col = new Array('rot', 'grün', 'blau');

for (let c in arr_col)
{
    console.log(arr_col[c]);
}

// new Array()
let arr = new Array(2);

arr[0] = 1;
arr[1] = "Ede";

for (let c in arr)
{
    console.log(arr[c]);
}

Mehrdimensionale Arrays

// Array in Literalschreibweise
let person = [
  {
    Name: "Udo",
    Alter: 30,
    Ort: "Dresden",
    Handys: {
      Samsung: "S9",
      Huawei: "P20"
    }
  },
  {
    Name: "Heinz",
    Alter: 40,
    Ort: "Berlin",
    Handys: {
      Samsung: "S8",
      Huawei: "P8 Lite"
    }
  },
  {
    Name: "Ede",
    Alter: 50,
    Ort: "Bonn",
    Handys: {
      Samsung: "S7",
      Huawei: "Nova"
    }
  }
];

person.forEach(function(p, a)
{
    console.log("Person: " + a);
                
    for (let attr in p)
    {
        if (attr != "Handys")
        {
          console.log("  " + attr + ": " + p[attr]);
        }
        else
        {
          console.log("    " + p[attr].Samsung );
          console.log("    " + p[attr].Huawei );
        }
    }
});