Home  >  Article  >  php教程  >  AngularJS operates key-value objects similar to java's hashmap

AngularJS operates key-value objects similar to java's hashmap

高洛峰
高洛峰Original
2016-12-07 10:05:391446browse

Foreword:

We know that the most commonly used hashmap methods in Java are put(...), get(...) and remove() methods, so how to create (use) such an object in angularJS

Thinking Analysis:

We know that in Java, chain access and "[]" can be used to access a certain value of hashmap

Specific implementation:

Chain access:

.factory('ParamsServices', function () {
var params = {};
return {
get: function (key) {
return params.key;
},
put: function (key, object) {
params.key = object;
},
remove: function (key) {
delete params.key;
}
};
})

"[]" access:

.factory('iParamsServices', function () {
var map = {};
return {
get: function (key) {
return map[key];
},
put: function (key, object) {
map[key] = object;
},
remove: function (key) {
delete map[key];
}
};
})

4. Verify

1. Write

ParamsServices.put("itv", "itv");
ParamsServices.put("itv2", "itv2");
iParamsServices.put("itv3", "itv3");
iParamsServices.put("itv4", "itv4");

2. Read

ParamsServices.get("itv") == ParamsServices.get("itv2") // true
iParamsServices.get("itv3") == iParamsServices.get("itv4") // false

5. Summary:

angul as key value object ( hashmap) recommended method Second, realize


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn