Skip to content

Instantly share code, notes, and snippets.

@ajay-ag
Created September 4, 2020 06:01
Show Gist options
  • Save ajay-ag/1fe0c7f8d5a78590a774acf210f7ab28 to your computer and use it in GitHub Desktop.
Save ajay-ag/1fe0c7f8d5a78590a774acf210f7ab28 to your computer and use it in GitHub Desktop.
Search And Filter array for vue.js
//productList = array of object
let productList = [{
Price: 500 , ProductName : "Test"
},{
Price: 200 , ProductName : "Test -2"
}];
if (this.searchKey != "") {
productList = this.products.filter((post) => {
return post.ProductName.toLowerCase().includes(
this.searchKey.toLowerCase()
);
});
}
if (this.short == "high") {
productList = productList.sort(
(a, b) => parseFloat(b.Price) - parseFloat(a.Price)
);
}
if (this.short == "low") {
productList = productList.sort(
(a, b) => parseFloat(a.Price) - parseFloat(b.Price)
);
}
if (this.short == "a-z") {
productList = productList.sort(function (a, b) {
if (a.ProductName < b.ProductName) return -1;
else if (a.ProductName > b.ProductName) return 1;
return 0;
});
}
if (this.short == "z-a") {
productList = productList.sort(function (a, b) {
if (a.ProductName > b.ProductName) return -1;
else if (a.ProductName < b.ProductName) return 1;
return 0;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment