5.8 object functions
This commit is contained in:
		
							
								
								
									
										0
									
								
								5.8objectkeys/main.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								5.8objectkeys/main.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										44
									
								
								5.8objectkeys/t.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								5.8objectkeys/t.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | |||||||
|  |  | ||||||
|  | console.log("====================object.keys, values, entries====================") | ||||||
|  |  | ||||||
|  | console.log("Object function, you must call Object.keys but not obj.keys") | ||||||
|  | console.log("Object.keys(obj) - returns an array of keys") | ||||||
|  | console.log("Object.values(obj) - returns an array of values") | ||||||
|  | console.log("Object.entries(obj) - returns an array of [key, value] pair") | ||||||
|  | console.log("Object.fromEntries(array) - on the resulting array to turn it back into an object") | ||||||
|  |  | ||||||
|  | let prices = { | ||||||
|  |     banana : 4, | ||||||
|  |     orange : 2, | ||||||
|  |     meat : 1, | ||||||
|  | } | ||||||
|  | console.log(`Object.entries(prices): ${Object.entries(prices)}`) | ||||||
|  |  | ||||||
|  | let doubleprices = Object.fromEntries( | ||||||
|  |     Object.entries(prices).map(entry => [entry[0], entry[1] * 2]) | ||||||
|  | ); | ||||||
|  | console.log(`Object.entries(doubleprices): ${Object.entries(doubleprices)}`) | ||||||
|  |  | ||||||
|  | Object.values(prices).reduce((sum, cur)=> sum+cur, 0) | ||||||
|  |  | ||||||
|  | function sumSalaries(salaries ){ | ||||||
|  |     return Object.values(salaries).reduce((sum, cur) => sum + cur, 0); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | let salaries = { | ||||||
|  |     "John": 100, | ||||||
|  |     "Pete": 300, | ||||||
|  |     "Mary": 250 | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | console.log(sumSalaries(salaries)); | ||||||
|  |  | ||||||
|  | function count(user){ | ||||||
|  |     return Object.keys(user).length; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | let user = { | ||||||
|  |     name : "John", | ||||||
|  |     age: 30 | ||||||
|  | }; | ||||||
|  | console.log(count(user)); | ||||||
		Reference in New Issue
	
	Block a user