hinzugefügt die Verwendung Object.values(), Object.key(), Object.entries() durch eines Objektes mit mehreren Eigenschaften
This commit is contained in:
parent
7c6e91e284
commit
5d451af3b2
@ -95,8 +95,15 @@ const restaurant = {
|
||||
},
|
||||
};
|
||||
|
||||
console.log(openingHours);
|
||||
for (const day of weekdays) {
|
||||
console.log(day);
|
||||
// Funktionalität vom nullish und optional chaning Operatoren
|
||||
// (man vermeindet eine Fehler-Ausgabe wenn ein Wert nicht existiert, als auch wenn ein Wert "0" ist)
|
||||
const open = restaurant.openingHours[day]?.open ?? "closed";
|
||||
console.log(`On ${day} we open at ${open}`);
|
||||
}
|
||||
|
||||
// console.log(openingHours);
|
||||
// console.log(restaurant.openingHours.sum?.open);
|
||||
|
||||
const menu = [...restaurant.starterMenu, ...restaurant.mainMenu];
|
||||
@ -112,3 +119,11 @@ for (const [index, item] of menu.entries()) {
|
||||
}
|
||||
// optional chaning Operator, gibt kein Fehler aus, wenn eine Eigenschaft/Variable/Wert eines Objektes nicht existiert
|
||||
console.log(restaurant.openingHours.mon?.open);
|
||||
|
||||
const users = [];
|
||||
|
||||
// längere Version der Überprüfung
|
||||
if (users.length > 0) console.log(users[0].name);
|
||||
else console.log("User array is empty");
|
||||
// kurzere Version Mithilfe des Optional chaining Operators und mit dem nullish Operator
|
||||
console.log(users[0]?.name ?? "User array is empty");
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Destructuring Objects</title>
|
||||
<script src="challenge1.js"></script>
|
||||
<script src="loopingTroughObjects.js"></script>
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
||||
|
||||
@ -0,0 +1,57 @@
|
||||
const weekdays = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
||||
|
||||
const openingHours = {
|
||||
[weekdays[3]]: {
|
||||
open: 12,
|
||||
close: 22,
|
||||
},
|
||||
[weekdays[4]]: {
|
||||
open: 11,
|
||||
close: 23,
|
||||
},
|
||||
[weekdays[5]]: {
|
||||
open: 0, // Open 24 hours
|
||||
close: 24,
|
||||
},
|
||||
};
|
||||
|
||||
const restaurant = {
|
||||
name: "Classico Italiano",
|
||||
location: "Via Angelo Tavanti 23, Firenze, Italy",
|
||||
categories: ["Italian", "Pizzeria", "Vegetarian", "Organic"],
|
||||
starterMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"],
|
||||
mainMenu: ["Pizza", "Pasta", "Risotto"],
|
||||
order: function (starterIndex, mainIndex) {
|
||||
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
|
||||
},
|
||||
// man kann ein Objekt außerhalb eines anderen Objectes definieren, und es nachträglich bei einem anderen Objekt verwenden
|
||||
openingHours,
|
||||
orderDelivery: function ({
|
||||
starterIndex = 1,
|
||||
mainIndex = 0,
|
||||
time = "20:00",
|
||||
address,
|
||||
}) {
|
||||
console.log(
|
||||
`Order recieved! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}`
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
for (const day of Object.keys(restaurant)) {
|
||||
console.log(day);
|
||||
}
|
||||
// die Ausgabe der Eigenschaften eines Objektes nach dem Schllüsel(key)
|
||||
const keyProperties = Object.keys(openingHours);
|
||||
console.log(keyProperties);
|
||||
// die Ausgabe der Eigenschaften eines Objektes nach dem Wert(value)
|
||||
const valueProperties = Object.values(openingHours);
|
||||
console.log(valueProperties);
|
||||
|
||||
// die Ausgabe des Eigenschaften eines Objektes nach dem Objekt(key,value)
|
||||
const objectProperties = Object.entries(openingHours);
|
||||
console.log(objectProperties);
|
||||
|
||||
for (const [day, { open, close }] of objectProperties) {
|
||||
console.log(`On ${day} we open at ${open} and close at ${close}`);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user