三种数据结构
首先看看这三种数据结构:
第一种,只存一个数组:
activities = [ { name: "first activity", sign_ups:[], bids:[] }, { name: "second activity", sign_ups: [ { name:"仝键", phone:"13600000000" } ], bids:[ { name:"竞价1", biddings : [ { name: "仝键", phone:"13600000000", price: "12" } ] } ] } ]
第二种,利用哈希存储数据:
activities = { "0":{ name: "first activity", sign_ups:[], bids:[], biddings:{} }, "1": { name: "second activity", sign_ups: [ { name:"仝键", phone:"13600000000" } ], bids:["竞价1","竞价2"], biddings:{ "竞价1":[ { phone:"13600000000", price: "12" } ], "竞价2": [ { phone:"13600000000", price: "10" } ] } } }
第三种,分成几个数组,层次少了:
activities = [ { id:"0", name: "first activity" }, { id:"1", name: "second activity" } ]; sign_ups = [ { name:"仝键", phone:"13600000000", activity_id:"0" },{ name:"仝", phone:"13600000000", activity_id:"1" } ] bids = [ { name: "竞价1", activity_id:"0", biddings:[ { phone:"13600000000", price: "9" } ] } ]
对于这三种结构,第一种只存一个数组,层次嵌套比较深,在存取数据时特别麻烦,要根据条件一层一层的遍历查找,比较耗内存,影响运行速度,但在信息匹配上,第一种还是挺方便的,不要太多的判断匹配。第二种是哈希的存储方式,与第一种相比,多出一个竞价报名的数组,在存取上优于第一种,稍微简单了。第三种数据结构,只有一层或两层,结构比较清晰,不需要繁琐的遍历查找,存取上很方便,但是在信息的匹配上会有一些麻烦,当时写时,在匹配信息上比较让我头疼。
关于这三种存储的方式,我比较喜欢第二种,利用哈希来存储,下面就来介绍一下哈希是如何存取的。
var a = {}; //定义一个变量为哈希 a.name = "moneyinto"; //第一种存储方式 a["age"] = "23"; //第二种存储方式 console.log(a); //object {name:"moneyinto",age:"23"} var b = a.name; //b的值为moenyinto a.age = "22"; //这样就直接更改了age属性对应的值了
哈希存的东西很多,不一定是字符串类型的或数值,它还可以存函数方法等,个人感觉不错。
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30