javascriptdojodstore

Dstore Filter: is there a way to compare properties?


Let's assume I have a store with different orders. The orders have a delivery address and an invoice address. The address itself has a city, street etc.:

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

I would like to filter all orders where the city of both the delivery address and the invoice address is the same.

I have tried that in a jsFiddle: https://jsfiddle.net/gbwv2bde/3/, but I am not so happy with the results. Does anybody know a way to use a Filter for that task?


Solution

  • Try this, it will return items Anna and 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);
            });
    

    Basically you can create a your own functions to pass to filter(), which you can use to write your own comparison logic.

    See here for more details 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: Example with more properties to compare. Your function just needs to return a true or false (true if the object matches your comparison conditions)

            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);
            });