eggplantSword's avatar

Use lodash to filter and sort collection

I'm trying to filter and then sort a collection of items to display, this is the computed method

 displayData() {
    let stor = this.sort.split(" ");

    this.filtered = _.orderBy(this.searching, [stor[0]], [stor[1]]);

    if (stor[0] === 'status') {
        this.filtered = _.find(this.searching, function (item) {
            return item.status === stor[1];
        });

        this.filtered = _.orderBy(this.searching, ['created_at'], [stor[2]]);
    }

    this.total = this.filtered.length;

    return this.filtered.slice(this.pageSize * this.page - this.pageSize, this.pageSize * this.page);
},

However this doesn't work, I get this error

Error in render: "TypeError: this.filtered.slice is not a function"

I also tried adding an else and doing the other code in there but get the same error, something to note as well, the regular let stor = this.sort.split(" ") was originally going to be something like this created_at desc but for the status its now going to sometimes be status order_request desc so now it's 3 instead of two, so that's why I created an if to check which option is being selected.

Some lodash docs https://lodash.com/docs/4.17.15#find and https://lodash.com/docs/4.17.15#orderBy

What am I doing wrong?

0 likes
2 replies
Sinnbeck's avatar

Well first of, you should check what filtered is set to

this.total = this.filtered.length;
console.log(this.filtered) //add this line and check the output in the console.
return this.filtered.slice(this.pageSize * this.page - this.pageSize, this.pageSize * this.page);

Please or to participate in this conversation.