Skip to content

Commit b198ff4

Browse files
committed
implemented array of objects example using forEach, map, filter, and reduce
1 parent 0e3b1a5 commit b198ff4

3 files changed

Lines changed: 207 additions & 0 deletions

File tree

array of objects/README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# array of objects
2+
An **array of objects** is a data structure where each element of the array is an object. This is useful for representing structured collections of related data.
3+
4+
## I practiced array of objects using the examples below:
5+
### Example 1:
6+
```javascript
7+
const fruits = [{name: "apple", colour: "green", price: 455},
8+
{name: "banana", colour: "yellow", price: 245},
9+
{name: "watermelon", colour: "purple", price: 355},
10+
{name: "grapes", colour: "red", price: 455},
11+
{name: "ovacado", colour: "green", price: 55},
12+
{name: "pineapple", colour: "yellow", price: 174}];
13+
14+
15+
console.log(typeof(fruits[0]));
16+
console.log(fruits[4]);
17+
console.log(fruits[0].name);
18+
fruits.pop();
19+
fruits.shift();
20+
fruits.push({name: "mango", colour: "yellow", price: 250});
21+
fruits.unshift({name: "guava", colour: "red", price: 700});
22+
console.log(fruits);
23+
24+
// forEach
25+
fruits.forEach(fruit => console.log(fruit.name));
26+
27+
// .map()
28+
const fruitNames = fruits.map(fruit => fruit.name);
29+
console.log(fruitNames);
30+
31+
// .filter()
32+
const yellowFruits = fruits.filter(fruit => fruit.colour === "yellow");
33+
console.log(yellowFruits);
34+
const highPrice = fruits.filter(fruit => fruit.price >= 500);
35+
console.log(highPrice);
36+
37+
// reduce
38+
const minFruit = fruits.reduce((min, next) =>
39+
next.price < min.price ?
40+
next : min);
41+
console.log(minFruit);
42+
43+
// reduce example 2
44+
const totalCost = fruits.reduce((sum, next) => sum + next.price, 0);
45+
console.log(totalCost);
46+
```
47+
48+
#### Console COdes and Ouput
49+
##### typeof(fruits[0])
50+
```javascript
51+
console.log(typeof(fruits[0]));
52+
// Output:
53+
object
54+
```
55+
56+
##### console.log(fruits[4])
57+
```javascript
58+
console.log(fruits[4]);
59+
// Output:
60+
{ name: 'ovacado', colour: 'green', price: 55 }
61+
```
62+
##### Accessing a single property
63+
```javascript
64+
console.log(fruits[0].name);
65+
// Output:
66+
apple
67+
```
68+
69+
##### Array after pop, shift, push, unshift
70+
```javascript
71+
console.log(fruits);
72+
// Output:
73+
[
74+
{ name: 'guava', colour: 'red', price: 700 },
75+
{ name: 'banana', colour: 'yellow', price: 245 },
76+
{ name: 'watermelon', colour: 'purple', price: 355 },
77+
{ name: 'grapes', colour: 'red', price: 455 },
78+
{ name: 'ovacado', colour: 'green', price: 55 },
79+
{ name: 'mango', colour: 'yellow', price: 250 }
80+
]
81+
```
82+
83+
##### .forEach() to print names
84+
```javascript
85+
fruits.forEach(fruit => console.log(fruit.name));
86+
// Output:
87+
guava
88+
banana
89+
watermelon
90+
grapes
91+
ovacado
92+
mango
93+
```
94+
95+
##### .map() to get array of names
96+
```javascript
97+
console.log(fruits.map(fruit => fruit.name));
98+
// Output:
99+
[ 'guava', 'banana', 'watermelon', 'grapes', 'ovacado', 'mango' ]
100+
```
101+
102+
##### .filter(): yellow fruits
103+
```javascript
104+
console.log(fruits.filter(fruit => fruit.colour === "yellow"));
105+
// Output:
106+
[
107+
{ name: 'banana', colour: 'yellow', price: 245 },
108+
{ name: 'mango', colour: 'yellow', price: 250 }
109+
]
110+
```
111+
112+
##### .filter(): high price (≥ 500)
113+
```javascript
114+
console.log(fruits.filter(fruit => fruit.price >= 500));
115+
// Output:
116+
[
117+
{ name: 'guava', colour: 'red', price: 700 }
118+
]
119+
```
120+
121+
##### .reduce(): lowest price fruit
122+
```javascript
123+
console.log(fruits.reduce((min, next) => next.price < min.price ? next : min));
124+
// Output:
125+
{ name: 'ovacado', colour: 'green', price: 55 }
126+
```
127+
128+
##### .reduce(): total price of all fruits
129+
```javascript
130+
console.log(fruits.reduce((sum, next) => sum + next.price, 0));
131+
// Output:
132+
2055
133+
```
134+
135+
### What I Did
136+
- I created an array of fruit objects with different names, colours, and prices.
137+
- I accessed specific fruit data using indexing and dot notation.
138+
- I modified the array using *pop(), shift(), push(), and unshift()*.
139+
- I used *.forEach()* to loop through and print all fruit names.
140+
- I applied *.map()* to extract an array of fruit names.
141+
- I used *.filter()* to get fruits based on specific conditions (e.g. yellow colour or price ≥ 500).
142+
- I used .*reduce()* to:
143+
1. Find the fruit with the lowest price
144+
2. Calculate the total cost of all fruits
145+
146+
Through this, I deepened my understanding on manipulating and working with array and objects.
147+
148+
## What I learnt from the two examples
149+
1. I learnt how to store and manage structured data using arrays of objects.
150+
2. I practiced using JavaScript's most common and useful array methods for:
151+
- Iteration (*forEach*)
152+
- Transformation (*map*)
153+
- Filtering (*filter*)
154+
- Aggregation (*reduce*)
155+
156+
This practice reinforced my understaing on loops, arrays and objects.

array of objects/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>array of objects</title>
7+
</head>
8+
<body>
9+
<script src="index.js"></script>
10+
</body>
11+
</html>

array of objects/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
const fruits = [{name: "apple", colour: "green", price: 455},
3+
{name: "banana", colour: "yellow", price: 245},
4+
{name: "watermelon", colour: "purple", price: 355},
5+
{name: "grapes", colour: "red", price: 455},
6+
{name: "ovacado", colour: "green", price: 55},
7+
{name: "pineapple", colour: "yellow", price: 174}];
8+
9+
10+
console.log(typeof(fruits[0]));
11+
console.log(fruits[4]);
12+
console.log(fruits[0].name);
13+
fruits.pop();
14+
fruits.shift();
15+
fruits.push({name: "mango", colour: "yellow", price: 250});
16+
fruits.unshift({name: "guava", colour: "red", price: 700});
17+
console.log(fruits);
18+
19+
// forEach
20+
fruits.forEach(fruit => console.log(fruit.name));
21+
22+
// .map()
23+
const fruitNames = fruits.map(fruit => fruit.name);
24+
console.log(fruitNames);
25+
26+
// .filter()
27+
const yellowFruits = fruits.filter(fruit => fruit.colour === "yellow");
28+
console.log(yellowFruits);
29+
const highPrice = fruits.filter(fruit => fruit.price >= 500);
30+
console.log(highPrice);
31+
32+
// reduce
33+
const minFruit = fruits.reduce((min, next) =>
34+
next.price < min.price ?
35+
next : min);
36+
console.log(minFruit);
37+
38+
// reduce example 2
39+
const totalCost = fruits.reduce((sum, next) => sum + next.price, 0);
40+
console.log(totalCost);

0 commit comments

Comments
 (0)