Working with JavaScript Arrays and Dictionaries
How do I make an working with javascript arrays and dictionaries?
What is a working with javascript arrays and dictionaries? How do you make a working with javascript arrays and dictionaries? This script and codes were developed by Jek Bao Choo on 19 January 2023, Thursday.
Working with JavaScript Arrays and Dictionaries - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Working with JavaScript Arrays and Dictionaries</title>
</head>
<body> <h1> Working with JavaScript Arrays and Dictionaries </h1> References
<li> Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript by David Herman
</li> <script src="js/index.js"></script>
</body>
</html>
Working with JavaScript Arrays and Dictionaries - Script Codes JS Codes
/*
* Item 43: Build Lightweight Dictionaries from Direct Instances of Object
* It's because other ways of doing it is vulnerable to prototype pollution hence affecting
* the results.
* By using a direct instance of Object, we localise the risk of Object.prototype alone. And nobody should ever add properties to Object.prototype
*/
var dict = { alice: 34, bob: 24
}; //this is object literation
var people = []; //this is array literation
for (var peopleName in dict) { people.push(peopleName + ": " + dict[peopleName]);
}
people; // [ 'alice: 34', 'bob: 24' ]
dict.chris = 62; //this is called 'update'. it's the same same having this inside the object literation above
"alice" in dict; //This is called 'membership test'
dict.alice; //This is called 'retrieval'
//We definitely can start dict as an empty object literal e.g.
var dict2 = {};
//In brief, we should use object literals to construct lightweight dictionaries
/*
* Item 46: Prefer Arrrays to Dictionaries for Ordered Collections
* When we need order of entries in a data structure, we use an array instead of a dictionary
*/
var arrayOfObjects = [ {name: "Hank", points: 1110100}, {name: "Steve", points: 1064500}, {name: "Billy", points: 1050200} ];
function reportBad(highScores) { var result = ""; var i = 1; for (var key in highScores) { result += i + ". " + key + ": " + highScores[key].name + " "; i++; } //key in question is the index of the array 0, 1, 2 return result;
}
reportBad(arrayOfObjects); //'1. 0: [object Object]\n2. 1: [object Object]\n3. 2: [object Object]\n'
//for...in loop enumerate the properties of the object in different orders, potentially jumbling the order of the report. We can see it clearly from the above example. Below we will see more with floating points examples
function reportGood(highScores) { var result = ""; for (var i = 0, n = highScores.length; i < n; i++) { var score = highScores[i]; result += (i + 1) + ". " + score.name + ": " + score.points + " "; } return result;
}
reportGood(arrayOfObjects);//'1. Hank: 1110100 2. Steve: 1064500 3. Billy: 1050200 '
//reportGood iterates over the elements in a precise order: from 0 to highScores.length - 1
/*A good example of order dependencies is floating point arithmetic*/
var ratings = { "Very good hunt" : 0.8, "Cry me a river" : 0.7, "x21" : 0.6, "Double Agent" : 0.9
}; //if we remove "x21" making it "21". it would generate 0.7499999999999999
//when it's "x21" it would generate 0.75. so why? The explanation is below
var total = 0, count = 0;
for (var key in ratings) { total += ratings[key]; count++;
} //unpredictable order
count;
total /= count;
total; // 0.75 or 0.749999999999
/*
The engine always enumerate potential array indices first before any other keys. Since the movie "21" happens to have a name that is a viable array index, it gets enumerated first resulting in
(0.6 + 0.8 + 0.7 + 0.9) / 4 = 0.7499999999999
Where as when the movie is "x21" it would be enumerated as (0.8 + 0.7 + 0.6 + 0.9) / 4 = 0.75
*/
Developer | Jek Bao Choo |
Username | jek |
Uploaded | January 19, 2023 |
Rating | 3 |
Size | 2,775 Kb |
Views | 6,072 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Using TauCharts | 2,315 Kb |
Understanding Dropzone.js | 2,925 Kb |
Bootstrap 4 nav-tabs | 1,523 Kb |
Rosen Center Nav | 3,677 Kb |
Imagining our data import steps | 3,095 Kb |
Working with JS Functions | 3,110 Kb |
Using ECharts | 2,888 Kb |
Playing with Bootstrap Column Ordering | 1,564 Kb |
Playing with Typeahead.js | 2,109 Kb |
Playing with w3schools Modals | 2,644 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Zip Button test | Lje7462 | 1,932 Kb |
C.Rowe Button | Brownerd | 2,473 Kb |
Blank Starter | Mhartington | 2,171 Kb |
Cloud upload | Jaflo | 2,774 Kb |
Random Gradients - JS | Aldlevine | 2,026 Kb |
DevCamp 2014 - Denver Public Library | See8ch | 5,033 Kb |
RequestAnimationFrame | Martin-banks | 2,541 Kb |
GsuiPopup | Mr21 | 2,641 Kb |
Underlined form fields | Mitchdot | 2,323 Kb |
RSW | JordanC | 3,726 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!