第一种数据结构:
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" } ] } ] } ];
第一种数据结构采用一个数组中存储两个 两个对象,每个对象又分别存储各自信息。其中第二个对象的结构为{name: "",sign_ups: "",bids: ""},biddings则存储在bids数组里,这种结构明显感觉嵌套比较多,结构清晰但存取或更改数据比较困难。
下面是创建一个新的竞价:
Bid.create_new_bid = function(name){ var bid = new Bid("first activity"); var activities = JSON.parse(localStorage.activities); activities = _.map(activities, function(activities){ activities.name == localStorage.current_activity ? activities.bids.push(bid) : ''; return activities }); localStorage.setItem('activities',JSON.stringify(activities)); }
数据层比较多,存储复杂。
第二种数据结构:
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" } ] } } };
第二种数据结构采用和第一种类似的结构,所有数据都存储在activities,activities中每个对象再分配一个id,总体格式是{{"0":""},{"1":""}},每个对象存储格式是{name: "",sign_ups: "",bids: "" , biddings: ""}
sign_ups中存储报名信息,biddings中存储竞价信息。存取都在一个数组里,比较清晰。
下面是根据phone取报名人姓名的方法:
Bidding.successful_of_people_name = function(activity, bid){ var activities = JSON.parse(localStorage.activities); var sign_ups=activities[activity].sign_ups; var phone = Bidding.count_price_first_information(activity, bid).phone var people_name = _.find(sign_ups,function(sign_ups){ return sign_ups.phone == phone }) return people_name.name }
第三种数据结构:
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_name: "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" } ] } ];
第三种数据结构定义了三个数组,分别是activities,sign_ups,bids。
特点是:数据分开存储,存取更加简单,但数据多且分散,结构不明显。
其中activities的结构是[{id:"",name:""}],每当创建一个活动时就相应给这个活动一个对应的id,可通过id更简单的存取数据。
下面是查找报名人姓名的方法:
Bidding.of_people_name = function(sms_json){ var sign_ups = JSON.parse(localStorage.sign_ups) return _.find(sign_ups,function(sign_up){ return sign_up.phone == sms_json.messages[0].phone && sign_up.activity_id == localStorage.current_activity }) }
相比于第二种能够明显体会到第三种数据结构存取方便但判断条件增加了。