|
| 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. |
0 commit comments