Created
September 4, 2020 06:01
-
-
Save ajay-ag/1fe0c7f8d5a78590a774acf210f7ab28 to your computer and use it in GitHub Desktop.
Search And Filter array for vue.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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