TaffyDB - 结合方法

时间:2015-05-28 16:00:29

标签: javascript distinct stringify taffydb

我是TaffyDB和JavaScript的新手。 如何组合TaffyDB中的方法?

我试图使用distinct ()stringify(),但无效

var xyz = db().distinct("mnths").stringify ();

console.log(xyz );

我收到错误消息,指出stringify()不受支持。

谢谢。

1 个答案:

答案 0 :(得分:1)

db().distinct("mnths")返回一个数组,而不是TaffyDB对象,因此.stringify()将无法使用它。

你可以改为

JSON.stringify(db().distinct("mnths"))

<强> This is what the documentation says

| Method          | Takes                     | Returns                                                                                   | Info                                                                                                                                    |
|-----------------|---------------------------|-------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| db().distinct() | One or more column names. | For one column: An array of values.For two or more columns: An array of arrays of values. | Used to select distinct values from the database for one or more columns. Pass in column names to get back an array of distinct values. |

&#13;
&#13;
var db = TAFFY([{
  "item":1,
    "name":"Blue Ray Player",
    "price":99.99,
    "type":"No"
}, {
    "item":2,
    name:"3D TV",
    price:1799.99,
    "type":"Yes"
}]);


var xyz = JSON.stringify(db().distinct("item"));
alert(xyz);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/taffydb/2.7.2/taffy-min.js"></script>
&#13;
&#13;
&#13;

更新

你可以做这样的事情

var filter = db().filter({ price: { gt : 49 } }); // filter data
var order = filter.order('price'); // this will sort 
var distinct = order.distinct('price'); // this will get distinct (at this stage TaffyDB spits out data so further chaining is not possible
var stringifiedData = JSON.stringify(distinct); // this will stringify the data
alert(stringifiedData);

&#13;
&#13;
var db = TAFFY(getData());

var filter = db().filter({
  price: {
    gt : 49
  }
});
var order = filter.order('price');
var distinct = order.distinct('price');
var stringifiedData = JSON.stringify(distinct);
alert(stringifiedData);


function getData() {
  return [{
    "item": 1,
    "name": "Gelato Topical Anesthetic",
    "price": 86,
    "type": false
  }, {
    "item": 2,
    "name": "Essential Shockeye and Lip",
    "price": 4,
    "type": true
  }, {
    "item": 3,
    "name": "Benazepril Hydrochloride",
    "price": 32,
    "type": false
  }, {
    "item": 4,
    "name": "Simulect",
    "price": 4,
    "type": false
  }, {
    "item": 5,
    "name": "Pollens - Weeds, Marshelder/Poverty Mix",
    "price": 59,
    "type": true
  }, {
    "item": 6,
    "name": "Carvedilol",
    "price": 88,
    "type": true
  }, {
    "item": 7,
    "name": "CardinalHealth bathing wipe with dimethicone",
    "price": 100,
    "type": true
  }, {
    "item": 8,
    "name": "Phentermine Hydrochloride",
    "price": 10,
    "type": false
  }, {
    "item": 9,
    "name": "NABUMETONE",
    "price": 14,
    "type": false
  }, {
    "item": 10,
    "name": "Members Mark Nicotine",
    "price": 82,
    "type": true
  }, {
    "item": 11,
    "name": "Amitriptyline Hydrochloride",
    "price": 13,
    "type": false
  }, {
    "item": 12,
    "name": "Oxygen",
    "price": 98,
    "type": true
  }, {
    "item": 13,
    "name": "buspirone hydrochloride",
    "price": 72,
    "type": true
  }, {
    "item": 14,
    "name": "Antibacterial Foaming Hand Sanitizer",
    "price": 94,
    "type": false
  }, {
    "item": 15,
    "name": "Nevirapine",
    "price": 61,
    "type": false
  }, {
    "item": 16,
    "name": "Tamsulosin Hydrochloride",
    "price": 39,
    "type": true
  }, {
    "item": 17,
    "name": "Aveeno Baby Daily Moisture",
    "price": 31,
    "type": false
  }, {
    "item": 18,
    "name": "Ultra Nutrition Ampoule",
    "price": 5,
    "type": true
  }, {
    "item": 19,
    "name": "Glytone essentials rejuvenate daily",
    "price": 36,
    "type": false
  }, {
    "item": 20,
    "name": "all day allergy relief",
    "price": 75,
    "type": true
  }, {
    "item": 21,
    "name": "CLE DE PEAU BEAUTE SILKY FOUNDATION I",
    "price": 7,
    "type": true
  }, {
    "item": 22,
    "name": "GABAPENTIN",
    "price": 48,
    "type": false
  }, {
    "item": 23,
    "name": "SUN Solar Defense Hydrating",
    "price": 44,
    "type": false
  }, {
    "item": 24,
    "name": "Carvedilol",
    "price": 38,
    "type": false
  }, {
    "item": 25,
    "name": "Metoprolol succinate",
    "price": 59,
    "type": false
  }, {
    "item": 26,
    "name": "Prosacea",
    "price": 38,
    "type": true
  }, {
    "item": 27,
    "name": "Russian Olive",
    "price": 50,
    "type": true
  }, {
    "item": 28,
    "name": "Silver Maple",
    "price": 33,
    "type": true
  }, {
    "item": 29,
    "name": "Dove Ultimate Visibly Smooth Nature Fresh",
    "price": 28,
    "type": true
  }, {
    "item": 30,
    "name": "Crayola Wild Blue Yonder Roll On Hand Sanitizer",
    "price": 89,
    "type": true
  }, {
    "item": 31,
    "name": "Oxygen",
    "price": 70,
    "type": false
  }, {
    "item": 32,
    "name": "infants fever reducer and pain reliever",
    "price": 80,
    "type": false
  }, {
    "item": 33,
    "name": "equaline childrens ibuprofen oral suspension",
    "price": 17,
    "type": false
  }, {
    "item": 34,
    "name": "Head and Shoulders Dual Sachets",
    "price": 80,
    "type": false
  }, {
    "item": 35,
    "name": "Kadian",
    "price": 22,
    "type": true
  }, {
    "item": 36,
    "name": "Fentanyl",
    "price": 90,
    "type": true
  }, {
    "item": 37,
    "name": "Vicks DayQuil and Vicks NyQuil",
    "price": 14,
    "type": false
  }, {
    "item": 38,
    "name": "Dr. Scholls",
    "price": 48,
    "type": true
  }, {
    "item": 39,
    "name": "Promethazine VC With Codeine",
    "price": 67,
    "type": false
  }, {
    "item": 40,
    "name": "Prestige",
    "price": 20,
    "type": true
  }, {
    "item": 41,
    "name": "propranolol hydrochloride",
    "price": 40,
    "type": false
  }, {
    "item": 42,
    "name": "KROGER",
    "price": 96,
    "type": false
  }, {
    "item": 43,
    "name": "Hydrochlorothiazide",
    "price": 16,
    "type": false
  }, {
    "item": 44,
    "name": "Herpes Sores",
    "price": 76,
    "type": true
  }, {
    "item": 45,
    "name": "TRICHOPHYTON MENTAGROPHYTES",
    "price": 10,
    "type": true
  }, {
    "item": 46,
    "name": "Promethazine Hydrochloride",
    "price": 45,
    "type": true
  }, {
    "item": 47,
    "name": "Allergy",
    "price": 27,
    "type": false
  }, {
    "item": 48,
    "name": "Hydrocortisone",
    "price": 69,
    "type": false
  }, {
    "item": 49,
    "name": "ChapStick Active Sport Ready",
    "price": 91,
    "type": false
  }, {
    "item": 50,
    "name": "Orange Pekoe Tea",
    "price": 25,
    "type": false
  }, {
    "item": 51,
    "name": "care one miconazole 7",
    "price": 63,
    "type": true
  }, {
    "item": 52,
    "name": "ACER NEGUNDO POLLEN",
    "price": 24,
    "type": false
  }, {
    "item": 53,
    "name": "Ceftriaxone",
    "price": 50,
    "type": true
  }, {
    "item": 54,
    "name": "Smart Sense Dual Action Complete",
    "price": 35,
    "type": true
  }, {
    "item": 55,
    "name": "Effexor",
    "price": 2,
    "type": false
  }, {
    "item": 56,
    "name": "Clopidogrel Bisulfate",
    "price": 82,
    "type": false
  }, {
    "item": 57,
    "name": "Methenamine Hippurate",
    "price": 89,
    "type": false
  }, {
    "item": 58,
    "name": "Glumetza",
    "price": 57,
    "type": false
  }, {
    "item": 59,
    "name": "Duloxetine",
    "price": 98,
    "type": true
  }, {
    "item": 60,
    "name": "Relaxatone",
    "price": 88,
    "type": false
  }, {
    "item": 61,
    "name": "Blues - Mood Support",
    "price": 89,
    "type": true
  }, {
    "item": 62,
    "name": "Phenytek",
    "price": 10,
    "type": true
  }, {
    "item": 63,
    "name": "LEUKERAN",
    "price": 20,
    "type": true
  }, {
    "item": 64,
    "name": "Citalopram",
    "price": 17,
    "type": true
  }, {
    "item": 65,
    "name": "ACTIFIRM RECOVERY EYE",
    "price": 34,
    "type": true
  }, {
    "item": 66,
    "name": "Potassium Chloride",
    "price": 99,
    "type": false
  }, {
    "item": 67,
    "name": "Mucus Relief",
    "price": 63,
    "type": false
  }, {
    "item": 68,
    "name": "Cold and Flu BP",
    "price": 52,
    "type": false
  }, {
    "item": 69,
    "name": "AQUAFRESH",
    "price": 10,
    "type": true
  }, {
    "item": 70,
    "name": "Buspirone Hydrochloride",
    "price": 100,
    "type": false
  }, {
    "item": 71,
    "name": "Childrens Allergy Relief",
    "price": 91,
    "type": false
  }, {
    "item": 72,
    "name": "PANCRELIPASE",
    "price": 96,
    "type": true
  }, {
    "item": 73,
    "name": "Hydrocodone Bitartrate and Acetaminophen",
    "price": 63,
    "type": false
  }, {
    "item": 74,
    "name": "Dicyclomine Hydrochloride",
    "price": 2,
    "type": false
  }, {
    "item": 75,
    "name": "clear lax",
    "price": 98,
    "type": false
  }, {
    "item": 76,
    "name": "Doxycycline",
    "price": 44,
    "type": false
  }, {
    "item": 77,
    "name": "NO-AD 45 Sunscreen",
    "price": 99,
    "type": false
  }, {
    "item": 78,
    "name": "Oxygen",
    "price": 36,
    "type": false
  }, {
    "item": 79,
    "name": "Spa Originals Antibacterial Gold Hand",
    "price": 26,
    "type": false
  }, {
    "item": 80,
    "name": "Citalopram",
    "price": 36,
    "type": true
  }, {
    "item": 81,
    "name": "BAMBOO SALT Eunganggo Jook Yeom Toothpaste",
    "price": 81,
    "type": false
  }, {
    "item": 82,
    "name": "Cefdinir",
    "price": 32,
    "type": true
  }, {
    "item": 83,
    "name": "Pravastatin Sodium",
    "price": 69,
    "type": true
  }, {
    "item": 84,
    "name": "Metoclopramide",
    "price": 61,
    "type": false
  }, {
    "item": 85,
    "name": "Loratadine and Pseudoephedrine",
    "price": 25,
    "type": true
  }, {
    "item": 86,
    "name": "Fluoxetine",
    "price": 52,
    "type": false
  }, {
    "item": 87,
    "name": "Good Sense Nicotine",
    "price": 53,
    "type": false
  }, {
    "item": 88,
    "name": "triamcinolone acetonide",
    "price": 78,
    "type": true
  }, {
    "item": 89,
    "name": "Warm Deep Original",
    "price": 60,
    "type": false
  }, {
    "item": 90,
    "name": "Pollens - Trees, Tree Mix 11",
    "price": 1,
    "type": false
  }, {
    "item": 91,
    "name": "Acidil",
    "price": 69,
    "type": true
  }, {
    "item": 92,
    "name": "Rugby GAS RELIEF",
    "price": 67,
    "type": false
  }, {
    "item": 93,
    "name": "Pandanus Calc. carb.",
    "price": 94,
    "type": false
  }, {
    "item": 94,
    "name": "Palgic",
    "price": 44,
    "type": false
  }, {
    "item": 95,
    "name": "Benzamycin Pak",
    "price": 41,
    "type": false
  }, {
    "item": 96,
    "name": "Labetalol Hydrochloride",
    "price": 31,
    "type": true
  }, {
    "item": 97,
    "name": "4 Way",
    "price": 36,
    "type": false
  }, {
    "item": 98,
    "name": "REPAIRWEAR ANTI -AGING MAKEUP",
    "price": 30,
    "type": false
  }, {
    "item": 99,
    "name": "leader cough dm",
    "price": 28,
    "type": true
  }, {
    "item": 100,
    "name": "Magnesium Oxide",
    "price": 82,
    "type": false
  }];
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/taffydb/2.7.2/taffy-min.js"></script>
&#13;
&#13;
&#13;