Dstore Filter:有没有办法比较属性?

Dstore Filter: is there a way to compare properties?

假设我有一家商店有不同的订单。订单有送货地址和发票地址。地址本身有城市、街道等:

let data = [
        { id: 1, name: 'Anna', delivery: {city: "Amsterdam"}, invoice: {city: "Amsterdam"} },
        { id: 2, name: 'Anton', delivery: {city: "Amsterdam"}, invoice: {city: "Berlin"}}
];

我想过滤所有送货地址和发票地址所在城市相同的订单。

我已经在 jsFiddle 中尝试过:https://jsfiddle.net/gbwv2bde/3/,但我对结果不太满意。有人知道为该任务使用过滤器的方法吗?

试试这个,它将 return 项 Anna 和 Julie

        var data = [
            { id: 1, name: 'Anna', delivery: { city: "Amsterdam" }, invoice: { city: "Amsterdam" } },
            { id: 2, name: 'Anton', delivery: { city: "Amsterdam" }, invoice: { city: "Berlin" } },
            { id: 3, name: 'John', delivery: { city: "Berlin" }, invoice: { city: "Paris" } },
            { id: 4, name: 'Julie', delivery: { city: "Paris" }, invoice: { city: "Paris" } }
        ];

        var myStore = new Memory({ data: data, idProperty: 'id' });
        var myResultsSet = myStore.filter(function (object) {
            return object.delivery.city === object.invoice.city;
        });

        myResultsSet.forEach(function (item) {
            console.log("item ", item.name);
        });

基本上您可以创建自己的函数传递给 filter(),您可以使用它来编写自己的比较逻辑。

详情请看这里 https://github.com/SitePen/dstore/blob/master/docs/Collection.md

filter(query)

This filters the collection, returning a new subset collection. The query can be an object, or a filter object, with the properties defining the constraints on matching objects. Some stores, like server or RQL stores, may accept string-based queries. Stores with in-memory capabilities (like dstore/Memory) may accept a function for filtering as well, but using the filter builder will ensure the greatest cross-store compatibility.

EDIT:具有更多要比较的属性的示例。你的函数只需要return一个真或假(如果对象符合你的比较条件则为真)

        var data = [
            { id: 1, name: 'Anna', delivery: { city: "Amsterdam", price: 5 }, invoice: { city: "Amsterdam", price: 20 } },
            { id: 2, name: 'Anton', delivery: { city: "Amsterdam", price: 8 }, invoice: { city: "Berlin", price: 7 } },
            { id: 3, name: 'John', delivery: { city: "Berlin", price: 10 }, invoice: { city: "Paris", price: 20 } },
            { id: 4, name: 'Julie', delivery: { city: "Paris", price: 2 }, invoice: { city: "Paris", price: 3  } }
        ];

        //example for custom filtering with nested properties
        var myStore = new Memory({ data: data, idProperty: 'id' });
        var myResultsSet = myStore.filter(function (object) {
            if(object.delivery.city === object.invoice.city){
                if (object.delivery.price < 5 && object.invoice.price < 5)
                    return true;
            }else
                return false;
        });

        myResultsSet.forEach(function (item) {
            console.log("item ", item.name);
        });