减少产品列表以仅显示最便宜的供应商

Reduce the list of products to only display the cheapest vendor

我正在使用适用于以下设置的 http://docs.couchdb.org/en/1.6.1/intro/tour.html 示例:

curl -X PUT http://127.0.0.1:5984/demo
curl -H 'Content-Type: application/json' -X POST http://127.0.0.1:5984/demo \
     -d '{"item": "apple","prices": {"Fresh Mart": 1.59,
                                     "Price Max": 5.99,
                                     "Apples Express": 0.79}}'
curl -H 'Content-Type: application/json' -X POST http://127.0.0.1:5984/demo \
     -d '{"item": "orange","prices": {"Fresh Mart": 1.99,
                                      "Price Max": 3.19,
                                      "Citrus Circus": 1.09}}'
curl -H 'Content-Type: application/json' -X POST http://127.0.0.1:5984/demo \
     -d '{"item": "banana","prices": {"Fresh Mart": 1.99,
                                      "Price Max": 0.79,
                                      "Banana Montana": 4.22}}'

它使用以下地图显示所有项目:

function(doc) {
  var shop, price, key;
  if (doc.item && doc.prices) {
      for (shop in doc.prices) {
          price = doc.prices[shop];
          key = [doc.item, price];
          emit(key, shop);
      }
  }
}

但它多次显示所有项目:

如何编写只显示每件商品的最便宜供应商的 reduce 函数?如何找到 "Where can I buy the cheapest apple?" 问题的答案?

reduce 函数不是执行此操作的正确工具。问题的答案是另一个地图功能。以下地图函数完成这项工作:

function(doc) {
  var shop, price, key;
  if (doc.item && doc.prices) {
    var cheapest = 99999999999;
    var cheapestshop = null;
    for (shop in doc.prices) {
      price = doc.prices[shop];
      if (price < cheapest) {
        cheapest = price;
        cheapestshop = shop;
      }
    }
    emit([doc.item, cheapest], cheapestshop);
  }
}