Working with JavaScript Arrays and Dictionaries

Developer
Size
2,775 Kb
Views
6,072

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 Previews

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
*/
Working with JavaScript Arrays and Dictionaries - Script Codes
Working with JavaScript Arrays and Dictionaries - Script Codes
Home Page Home
Developer Jek Bao Choo
Username jek
Uploaded January 19, 2023
Rating 3
Size 2,775 Kb
Views 6,072
Do you need developer help for Working with JavaScript Arrays and Dictionaries?

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!

Jek Bao Choo (jek) Script Codes
Create amazing sales emails with AI!

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!