AngularJS operation key-value objects are similar to java's hashmap (fill-in summary) and angularjshashmap

Source: Internet
Author: User

AngularJS operation key-value objects are similar to java's hashmap (fill-in summary) and angularjshashmap

Preface:

We know that the most commonly used hashmap in java is put (...), get (...) and how to create (use) such an object in angularJS

Train of Thought Analysis:

We know that in java, chained access and "[]" can be used to access a value of hashmap.

Specific implementation:

Chained 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") // trueiParamsServices.get("itv3") == iParamsServices.get("itv4") // false

5. Conclusion:

We recommend that you use method 2 to implement angul as a key value object (hashmap ).

The above section describes the AngularJS operation key-value object similar to the hashmap of java. I hope it will help you. If you have any questions, please leave a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.