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

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

WBOY
WBOY原創
2016-05-16 15:48:521129瀏覽

本文實例講述了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程式設計有所幫助。
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn