There are a lot of tutorials on the Internet, and you can search for the basic knowledge on your own.
The exercise is mainly based on orelly's book "data structure and algorithm JavaScript description.
See Code visible: https://github.com/dashnowords/blogs/tree/master/Structure/Dictionary
Basic dictionary knowledge
ToKey-value pairs
Data structure of data storage in the formJavascript
Objects are more directly used. Generally, the object constructed in this article is used only in scenarios with sorting requirements.Dictionary
Class, because the object attributes are unordered.
Dictionary Application
Dictionary
Javascript is one of the most commonly used technologies.Rule Mode
. Policy mode refers to defining a series of algorithms and encapsulating them one by one. Separating the unchanged and changing parts is the topic of each design pattern, and the policy pattern is no exception. The purpose of the policy pattern is to separate the use of the algorithm from the implementation of the algorithm. For example, the following example calculates the salary and bonus:
var obj = { "A": function(salary) { return salary * 4; }, "B" : function(salary) { return salary * 3; }, "C" : function(salary) { return salary * 2; } };var calculateBouns =function(level,salary) { return obj[level](salary);};console.log(calculateBouns(‘A‘,10000)); // 40000
Basic exercises
ConstructDictionary
Class. Implement the following basic methods:
add(key, value)
-- Add a record
find(key)
-- Find the value corresponding to the specified key
remove(key)
-- Remove a specified key-Value Pair
showAll( )
-- Display all key-value pairs
clear( )
-- Clear data
Exercises after class (the seventh exercise in the book)
- Write a program that reads the name and phone number from the text and stores it in a dictionary. The program includes the following functions: display a single phone number and display all phone numbers, add a new phone number, delete the phone number, and clear all the phone numbers.
- Use
Dictionary
Class to write a program to count the number of times each word appears in a piece of text. For example, "the brown for jumped over the Blue Fox", the corresponding output is:
the:2brown:1fox:2jumped:1over:1blue:1
- Modify Exercise 2 to display words alphabetically.
Basic wild front-end data structure exercises (4)-dictionary