三种数据结构
为了能够更加熟悉JavaScript这种语言的存储方式,于是就进行了三种数据结构的强化训练,从中了解三种数据结构的优缺点,以便能够找到合适的存储方式来实现项目的开发。
第一种数据结构的存储方式如下:
activities = [ { name: "first activity", sign_ups:[], bids:[] }, { name: "first activity", sign_ups: [ { name:"仝键", phone:"13600000000" }, { name:"于硕", phone:"15600000000" }, { name:"吴京川", phone:"13800000000" } ], bids:[ { name:"竞价1", biddings : [ { name: "仝键", phone:"13600000000", price: "12" }, { name: "于硕", phone:"15600000000", price: "10" } ] }, { name:"竞价2", biddings : [ { name: "仝键", phone:"13600000000", price: "10" }, { name: "于硕", phone:"15600000000", price: "12" }, { name: "吴京川", phone:"13800000000", price: "10" } ] } ] } ];
对于这种数据结构的存储方式,我感觉这种数据结构的嵌入太深,这种存储方式是一层嵌一层的形式,能够很容易的看出整个工程的存储形式,可以很容易的看出活动下的各种数组的存储,但是由于这种存储方式需要,对于小型的存储来说,能够很好的实现,但对于较大项目来说,这种方式嵌套太多,工作量大,在存取方面需要更多繁琐的操作。
为了取出较深层次的数组,需要多个_.find方法来实现这一存取,代码可如下所示:
var working_activity = _.find(jj_list,function(working){return working.name == localStorage.current_activity}) var working_name = _.find(working_activity.sign_ups,function(currenting){return currenting.phone == bid_phone})
第二种数据结构的存储方式如下:
localStorage.actity_ids = ["0","1"]; activities = { "0":{ name: "first activity", sign_ups:[], bids:[], biddings:{} }, "1": { name: "second activity", sign_ups: [ { name:"仝键", phone:"13600000000" }, { name:"于硕", phone:"15600000000" }, { name:"吴京川", phone:"13800000000" } ], bids:["竞价1","竞价2"], biddings:{ "竞价1":[ { phone:"13600000000", price: "12" }, { phone:"15600000000", price: "10" } ], "竞价2": [ { phone:"13600000000", price: "10" }, { phone:"15600000000", price: "12" }, { phone:"13800000000", price: "10" } ] } } };
相对于第一种方式来说,第二种采用哈希存储方式,通过获取需要存取数据的ID来实现对数据的存储,从而达到哈希存储的方式,哈希存取的代码如下:
var bm_jj_get = {phone: bid_phone,price: bid_price} jj_list[localStorage.current_activity_id].biddings[localStorage.current_bid].unshift(bm_jj_get) localStorage.setItem("activities",JSON.stringify(jj_list))
这种存取相对简单一些,只是逻辑方面可能需要花大量的功夫。
第三种数据结构的存储方式如下:
activities = [ { id:"0", name: "first activity" }, { id:"1", name: "second activity" } ]; sign_ups = [ { name:"仝键", phone:"13600000000", activity_id:"0" }, { name:"于硕", phone:"15600000000", activity_id:"0" }, { name:"吴京川", phone:"13800000000", activity_id:"0" }, { name:"仝", phone:"13600000000", activity_id:"1" }, { name:"于", phone:"15600000000", activity_id:"1" }, { name:"吴", phone:"13800000000", activity_id:"1" } ] bids = [ { name: "竞价1", activity_id:"0", biddings:[ { phone:"13600000000", price: "9" }, { phone:"15600000000", price: "10" } ] }, { name: "竞价1", activity_id:"1", biddings:[ { phone:"13600000000", price: "12" }, { phone:"15600000000", price: "10" } ] }, { name: "竞价2", activity_id:"1", biddings:[ { phone:"13600000000", price: "10" }, { phone:"15600000000", price: "12" }, { phone:"13800000000", price: "10" } ] } ];
这种数据存储结构一眼都能看出一个工程中有几个数组,因为这种数据结构是单独的进行存储的,相互之间通过id进行标示,来实现数组之间的联系。存储数据挺方便的,但是这种数据结构获取数据时,还需要进行逻辑的选择,以便获取想要取出的数据。
我在做party_bid时,使用的是第一种数据结构,在重构的时候,感觉逻辑上比较混乱,东西都不知道如何存取,因而我感觉就一个小项目party_bid的数据存储都有些混乱,所以我感觉第二种和第三种存取的方式相对较好一些,在以后的学习、工作中,我将会更多的使用第二和第三种数据结构。