三种数据结构
重构完成后做了一下三种数据结构,理解各种存储方式的优缺点.
第一种数据结构
var two_activities = [{ name: "first activity", sign_ups: [], bids: [] }, { name: "second 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" } ] }, ] }];
第一种数据把所有的数据存储到一个数组里,结构清晰,取数据也方便,但是取数据的时候需要经过多个for,和if的循环嵌套比较繁琐,如果数据较少的话可以这样存取,数据量大的话不推荐
2.第二种数据结构(哈希表)
var two_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" } ], } } };
第二种数据结构采用哈希表的方式存储,所有数据存储在activities中,每个活动都分配一个对应的id.结构清晰,存取也较为方便 举个例子,检查是否报名电话重复
check_bid_phone_repeat = function (sms_json) { var activities = JSON.parse(localStorage.getItem("activities")); var apply_phone = sms_json.messages[0].phone var activity = activities[localStorage.current_activity_id].biddings[localStorage.current_bid] return( _.find(activity, function (act) { return act.phone == apply_phone })) }
3.第三种数据结构
var bids = [ { name: "竞价1", activity_id: "1", biddings: [] } ]; 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" } ]
第三种数据结构每个信息存储成一个数组,存储数据的时候特别方便,结构不是很清晰,取相应数据的时候需要逻辑判断,并且存数据的是个个个表之间要有联系.
三种数据结构各有利弊,就个人而言感觉第二中和第三种比较方便,用第三种的时候逻辑一定要清楚
相关推荐
LauraRan 2020-09-28
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