hinzugefügt Daten transformation Mithilfe, map(), filter(), reduce() Funktionen
This commit is contained in:
parent
d303670a68
commit
52816cb770
@ -0,0 +1,90 @@
|
|||||||
|
const account1 = {
|
||||||
|
owner: "David Aster",
|
||||||
|
movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
|
||||||
|
interestRate: 1.2, // %
|
||||||
|
pin: 1111,
|
||||||
|
};
|
||||||
|
|
||||||
|
const account2 = {
|
||||||
|
owner: "Joanne Aster",
|
||||||
|
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
|
||||||
|
interestRate: 1.5,
|
||||||
|
pin: 2222,
|
||||||
|
};
|
||||||
|
|
||||||
|
const account3 = {
|
||||||
|
owner: "Nika Aster",
|
||||||
|
movements: [200, -200, 340, -300, -20, 50, 400, -460],
|
||||||
|
interestRate: 0.7,
|
||||||
|
pin: 3333,
|
||||||
|
};
|
||||||
|
|
||||||
|
const account4 = {
|
||||||
|
owner: "Eva Aster",
|
||||||
|
movements: [430, 1000, 700, 50, 90],
|
||||||
|
interestRate: 1,
|
||||||
|
pin: 4444,
|
||||||
|
};
|
||||||
|
|
||||||
|
const randomNumbers = function (n) {
|
||||||
|
let arr = [];
|
||||||
|
for (let i = 0; i < n; i++) {
|
||||||
|
arr.push(Math.trunc(Math.random() * 1000));
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
};
|
||||||
|
console.log("Ursprüngliche Werte");
|
||||||
|
var arr = randomNumbers(20);
|
||||||
|
// werden ursprüngliche Werte geändert, in unserem Fall curr * 2
|
||||||
|
console.log(arr);
|
||||||
|
arr = arr.map(function (curr) {
|
||||||
|
return curr * 2;
|
||||||
|
});
|
||||||
|
console.log("Geänderte Werte (map)");
|
||||||
|
console.log(arr);
|
||||||
|
|
||||||
|
arr = randomNumbers(20);
|
||||||
|
// Predikat in der return Anweisung, damit werden Werte, die der Bedienung nicht entsprechen, aus dem Feld enfernt
|
||||||
|
console.log("Ursprüngliche Werte");
|
||||||
|
console.log(arr);
|
||||||
|
arr = arr.filter(function (value) {
|
||||||
|
return value > 450;
|
||||||
|
});
|
||||||
|
console.log("Geänderte Werte (filter > 450)");
|
||||||
|
console.log(arr);
|
||||||
|
|
||||||
|
arr = randomNumbers(20);
|
||||||
|
console.log("Ursprüngliche Werte");
|
||||||
|
console.log(arr);
|
||||||
|
// Berechnung aller Werte
|
||||||
|
arr = arr.reduce(function (prev, curr) {
|
||||||
|
return prev + curr;
|
||||||
|
});
|
||||||
|
// Array wird in einen Wert konvertiert
|
||||||
|
console.log(arr);
|
||||||
|
|
||||||
|
const user = "David Steven Schmidt Aster";
|
||||||
|
// Abkürzung der Namen Mithilfe string Funtionen (Function chaining)
|
||||||
|
const createUsername = function (user) {
|
||||||
|
return user
|
||||||
|
.toLowerCase()
|
||||||
|
.split(" ")
|
||||||
|
.map(name => name[0])
|
||||||
|
.join("");
|
||||||
|
};
|
||||||
|
console.log(createUsername(user));
|
||||||
|
|
||||||
|
const accounts = [account1, account2, account3, account4];
|
||||||
|
|
||||||
|
const createUsernames = function (accounts) {
|
||||||
|
accounts.forEach(function (account) {
|
||||||
|
account.username = account.owner
|
||||||
|
.toLowerCase()
|
||||||
|
.split(" ")
|
||||||
|
.map(name => name[0])
|
||||||
|
.join("");
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
createUsernames(accounts);
|
||||||
|
console.log(accounts);
|
||||||
14
javascript/Javascript Expert/chapter11/forEachWithMaps.js
Normal file
14
javascript/Javascript Expert/chapter11/forEachWithMaps.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
const currencies = new Map([
|
||||||
|
["USD", "United States dollar"],
|
||||||
|
["EUR", "Euro"],
|
||||||
|
["GBP", "Pound sterling"],
|
||||||
|
]);
|
||||||
|
|
||||||
|
currencies.forEach(function (value, key, map) {
|
||||||
|
console.log(`${key}: ${value}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
const currency1 = new Set(["USD", "EUR", "GBP"]);
|
||||||
|
currency1.forEach(function (key, value, set) {
|
||||||
|
console.log(`${key}: ${value} `);
|
||||||
|
});
|
||||||
13
javascript/Javascript Expert/chapter11/map.js
Normal file
13
javascript/Javascript Expert/chapter11/map.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
const randomNumbers = n => {
|
||||||
|
let arr = [];
|
||||||
|
for (let i = 0; i < n; i++) {
|
||||||
|
arr.push(Math.trunc(Math.random() * 1000));
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
};
|
||||||
|
var arr = randomNumbers(15);
|
||||||
|
console.log("EUR", arr);
|
||||||
|
|
||||||
|
const eurToUsd = 1.2;
|
||||||
|
arr = arr.map(that => Math.trunc(that * eurToUsd));
|
||||||
|
console.log("Konvertiert ins USD", arr);
|
||||||
Loading…
x
Reference in New Issue
Block a user