字典是一种以键值对存在的数据结构,他的底层是Array数组

字典初始化和数组初始化的区别:

数组的初始化:var arr = [1,2,3,4,5];//使用中括号
字典的初始化: var names = {“a”:“aaa”,“b”:“bbb”,“c”:“ccc”};//使用花括号

字典的创建
//创建方式1
var arr = {"a":"1","b":"2","c":"3"};

//创建方式2
var arr = new Array;
arr["a"] = "1";
arr["b"] = "2";
arr["c"] = "3";
arr["d"] = "4";
字典的遍历
for (var key in arr) {
console.log(key+":"+arr[key]);
}
删除字典中的元素
//1.
delete arr["a"];
delete arr["b"];
//2.
delete arr.a;
delete arr.b;
//注意使用第二种方法删除时,arr后面只能是字符。
按key或value对字典的排序:
//按key进行排序
var arr = {"a":"1","b":"2","c":"3","d":"4"};

var ret = Object.keys(arr).sort(function(a,b){return a-b;});

for (var key in ret) {//字典的遍历
console.log(ret[key]+":"+arr[ret[key]]);
}

//按value进行排序
var arr = {"a":"1","b":"2","c":"3","d":"4"};

var ret = Object.keys(arr).sort(function(a,b){return arr[a]-arr[b];});

for (var key in ret) {//字典的遍历
console.log(ret[key]+":"+arr[ret[key]]);
}