- Mastering Ethereum
- Merunas Grincalaitis
- 139字
- 2021-06-24 15:01:06
Mappings
Mappings are a special type of variable in the sense that they can hold an endless amount of data. It's like a combination of an array and a struct. You can add elements to it for a set of types:
mapping(string => bool) public validStrings;
Mappings store information as an unlimited array. They work similar to objects in JavaScript, where each key has a value associated and they can be accessed randomly. They don't have a fixed length, nor can you get the length of them, as with arrays, for looping their values. What you must do instead is save the latest updated key of your mapping and go from there.
You can set values for mappings as follows:
validStrings['example'] = true;
In our example, all values of validStrings will be false until you set them to true.