首頁 > web前端 > js教程 > JavaScript資料庫TaffyDB用法實例分析_javascript技巧

JavaScript資料庫TaffyDB用法實例分析_javascript技巧

WBOY
發布: 2016-05-16 15:48:52
原創
1172 人瀏覽過

本文實例講述了JavaScript資料庫TaffyDB用法。分享給大家供大家參考。具體如下:

TaffyDB 是一個免費開源的 JavaScript 函式庫,用於在 Web 上實作一個輕量級的資料存取層,也就是一個簡單的資料庫。

資料定義:

var friends = new TAFFY(
[
{name:"Bob",
 gender:"M",
 married:"No",
 age:25,
 state:"NY",
 favorite_foods:["pizza","tacos"]},
 {name:"Joyce",
 gender:"F",
 married:"No",
 age:29,
 state:"WA",
 favorite_foods:["salad","cheese sticks"]},
 {name:"Dan",
 gender:"M",
 married:"No",
 age:29,
 state:"MT",
 favorite_foods:["pizza","hamburgers","BLTs"]},
 {name:"Sarah",
 gender:"F",
 married:"No",
 age:21,
 state:"ID",
 favorite_foods:["pizza","sushi"]}
 ]
)

登入後複製

查詢:

friends.find({age:{greaterthan:22}});
friends.find({state:["WA","MT","ID"]});
friends.find({state:["WA","MT","ID"],
       age:{greaterthan:22}});

登入後複製

更新操作:

friends.update(
  {
  state:"CA",
  married:"Yes"
  },
  {
  name:"Joyce"
  }
  );
friends.update({state:"CA",married:"Yes"},1);
friends.update(
  {
  state:"CA",
  married:"Yes"
  },
  friends.find(
    {name:"Joyce"}
    )
  );

登入後複製

插入資料:

//Inserting is simple and works as you would expect:
friends.insert(
  {name:"Brian",
  gender:"M",
  married:"No",
  age:52,
  state:"FL",
  favorite_foods:["fruit","steak"]
  });

登入後複製

刪除:

複製程式碼 程式碼如下:
friends.remove({name:"Brian"});

排序:
friends.orderBy(["age",{"name":"desc"}]);
var keys = new TAFFY([
{name:"12abc"},
{name:"abc343"},
{name:"1abc"},
{name:"23abc"}
]);
keys.orderBy({name:"logical"});

登入後複製

forEach用法:
friends.forEach(function (f,n) {alert(f.name)});
friends.forEach(
  function (f,n) {alert(f.name);},
  {favorite_foods:{has:"pizza"}}
);

登入後複製

希望本文所述對大家的javascript程式設計有所幫助。
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板