Dynamically creating keys/values in JavaScript associative array
来源:互联网 发布:杀破狼js原版百度云 编辑:程序博客网 时间:2024/04/30 23:51
http://stackoverflow.com/questions/351495/dynamically-creating-keys-in-javascript-associative-array
Somehow all examples, while work well, are overcomplicated:
- They use
new Array()
, which is an overkill (and an overhead) for a simple associative array (AKA dictionary). - The better ones use
new Object()
. Works fine, but why all this extra typing?
This question is tagged "beginner", so let's make it simple.
The uber-simple way to use a dictionary in JavaScript or "Why JavaScript doesn't have a special dictionary object?":
// create an empty associative array (in JavaScript it is called ... Object)var dict = {}; // huh? {} is a shortcut for "new Object()"// add a key named fred with value 42dict.fred = 42; // we can do that because "fred" is a constant // and conforms to id rules// add a key named 2bob2 with value "twins!"dict["2bob2"] = "twins!"; // we use the subscript notation because // the key is arbitrary (not id)// add an arbitrary dynamic key with a dynamic valuevar key = ..., // insanely complex calculations for the key val = ...; // insanely complex calculations for the valuedict[key] = val;// read value of "fred"val = dict.fred;// read value of 2bob2val = dict["2bob2"];// read value of our cool secret keyval = dict[key];
Now let's change values:
// change the value of freddict.fred = "astra";// the assignment creates and/or replaces key-value pairs// change value of 2bob2dict["2bob2"] = [1, 2, 3]; // any legal value can be used// change value of our secret keydict[key] = undefined;// contrary to popular beliefs assigning "undefined" does not remove the key// go over all keys and values in our dictionaryfor(key in dict){ // for-in loop goes over all properties including inherited properties // let's use only our own properties if(dict.hasOwnProperty(key)){ console.log("key = " + key + ", value = " + dict[key]); }}
Deleting values is easy too:
// let's delete freddelete dict.fred;// fred is removed, the rest is still intact// let's delete 2bob2delete dict["2bob2"];// let's delete our secret keydelete dict[key];// now dict is empty// let's replace it, recreating all original datadict = { fred: 42, "2bob2": "twins!" // we can't add the original secret key because it was dynamic, // we can only add static keys // ... // oh well temp1: val};// let's rename temp1 into our secret key:if(key != "temp1"){ dict[key] = dict.temp1; // copy the value delete dict.temp1; // kill the old key}else{ // do nothing, we are good ;-)}
0 0
- Dynamically creating keys/values in JavaScript associative array
- ruby sum values in array by keys
- Associative Array in Actionscript3 & PHP
- Creating and deleting threads dynamically in eCos
- Resetting an Associative array in PL/SQL?
- Dynamically Creating Bound and Template Columns in GridView
- Sort in Array with duplicate keys
- Oracle index by table(Associative array) used in PostgreSQL
- ypcat - print values of all keys in a NIS database
- Creating custom objects in JavaScript
- Creating ASP.NET Controls Dynamically
- Creating DataGrid Templated Columns Dynamically -
- Dynamically creating bound and template columns in GridView using ASP.Net
- Creating tables accelerator keys
- Array-In-JavaScript 数组
- JavaScript Associative Arrays
- Truthy and Falsy Values in JavaScript
- Truthy Vs Falsy Values in JavaScript
- windows 2008 R2 使用小结
- 2014年7月23日 6点15分
- CentOS系统时间与现在时间相差8小时解决方法
- 图片格式与设计那点事儿
- How do I store a current user context or global constants in Angular?
- Dynamically creating keys/values in JavaScript associative array
- day day up--Java通过JNI调用本地方法
- 独立博客大全的SWOT分析
- JAVA_字符串
- 序列化和反序列化的例子 程序中新建文件的操作
- HDU 4857 逃生(拓扑排序)
- 天津红桥西青静海面试题目
- GridView+pagerViewer 图片浏览,点击全屏,滑动左右切换+删除关闭+拍照
- hdu1010 dfs+路径剪枝