1const students = {
2 adam: {age: 20},
3 kevin: {age: 22},
4};
5
6Object.entries(students).forEach(student => {
7 // key: student[0]
8 // value: student[1]
9 console.log(`Student: ${student[0]} is ${student[1].age} years old`);
10});
11/* Output:
12Student: adam is 20 years old
13Student: kevin is 22 years old
14*/
1const list = {
2 key: "value",
3 name: "lauren",
4 email: "lauren@notreally.com",
5 age: 30
6};
7
8// Object.keys returns an array of the keys
9// for the object passed in as an argument.
10
11Object.keys(list).forEach(val => {
12 let key = val;
13 let value = list[val];
14 console.log(`${key} : ${value}`);
15});
16
17// Returns:
18// "key : value"
19// "name : lauren";
20// "email : lauren@notreally.com"
21// "age : 30"
22
1const object1 = {
2 a: 'somestring',
3 b: 42
4};
5for (let [key, value] of Object.entries(object1)) {
6 console.log(`${key}: ${value}`);
7}
8// expected output:
9// "a: somestring"
10// "b: 42"
11// order is not guaranteed
1const object = {a: 1, b: 2, c: 3};
2
3for (const property in object) {
4 console.log(`${property}: ${object[property]}`);
5}
1Object.entries(obj).forEach(
2 ([key, value]) => console.log(key, value)
3);
4
5// Loop using forEach
6arr.forEach((item, index) => {
7 // TODO
8})
9
10// Loop using for...of
11for (const item of arr) {
12 await something()
13}
14
15// Clone new array (not changing the old one)
16const arr = [1,2,3]
17const newArray = arr.map(item => item * 2)
18console.log(newArray)
19
20// Filter array by condition
21const arr = [1,2,3,1]
22const newArray = arr.filter(item => {
23 if (item === 1) { return item }
24})
25console.log(newArray)
26
27// Join array
28const arr1 = [1,2,3]
29const arr2 = [4,5,6]
30const arr3 = [...arr1, ...arr2]
31console.log(arr3)
32
33// Get a property of object
34const { email, address } = user
35console.log(email, address)
36
37// Copy object/array
38const obj = { name: 'my name' }
39const clone = { ...obj }
40console.log(obj === clone)
1const obj = {
2 a: 1,
3 b: 2,
4 c: 3
5};
6
7for (let key in obj) {
8 console.log(key + " = " + obj[key]);
9}
10// Ausgabe:
11// "a = 1"
12// "b = 2"
13// "c = 3"