Last active
August 3, 2016 16:47
-
-
Save tawanorg/e2a95629e6f680d06036b13d6fa88038 to your computer and use it in GitHub Desktop.
To filter data by color and capacity
This file contains 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
/** | |
* @package timApp | |
* @subpackage filterData | |
* @desc To filter data by color and capacity | |
* @author tim tawan <[email protected]> | |
**/ | |
var timApp = timApp || {}; | |
timApp.filterData = { | |
// Dummy data | |
data : function() { | |
// This function, in case you could improve by add fetching data from API | |
return [ | |
{ | |
"id": 1, | |
"name": "iPhone SE", | |
"color": "Silver", | |
"type": "Wifi", | |
"capacity": "16GB", | |
"price": 629 | |
}, | |
{ | |
"id": 2, | |
"name": "iPhone SE", | |
"color": "Silver", | |
"type": "Wifi", | |
"capacity": "64GB", | |
"price": 829 | |
}, | |
{ | |
"id": 3, | |
"name": "iPhone SE", | |
"color": "Gold", | |
"type": "Wifi", | |
"capacity": "16GB", | |
"price": 629 | |
}, | |
{ | |
"id": 4, | |
"name": "iPhone SE", | |
"color": "Gold", | |
"type": "Wifi", | |
"capacity": "64GB", | |
"price": 829 | |
}, | |
{ | |
"id": 5, | |
"name": "iPhone SE", | |
"color": "Space Grey", | |
"type": "Wifi", | |
"capacity": "16GB", | |
"price": 629 | |
}, | |
{ | |
"id": 6, | |
"name": "iPhone SE", | |
"color": "Space Grey", | |
"type": "Wifi", | |
"capacity": "64GB", | |
"price": 829 | |
}, | |
{ | |
"id": 7, | |
"name": "iPhone SE", | |
"color": "Rose Gold", | |
"type": "Wifi", | |
"capacity": "16GB", | |
"price": 629 | |
}, | |
{ | |
"id": 8, | |
"name": "iPhone SE", | |
"color": "Rose Gold", | |
"type": "Wifi", | |
"capacity": "64GB", | |
"price": 829 | |
} | |
]; | |
}, | |
// Complete the functions below | |
filterBySearchTerm: function(searchTerm) { | |
// Place object data to new array | |
var datas = this.data(); | |
var keywords = searchTerm; | |
var objectSearch = { | |
searchTerm: keywords | |
} | |
return datas.filter(this.doFilter, objectSearch); | |
}, | |
// Do filter | |
doFilter: function(value) { | |
if(typeof value == 'object' || this.searchTerm != undefined) { | |
var capacity = value.capacity; | |
var color = value.color.toLowerCase(); | |
var keyword = this.searchTerm.toLowerCase(); | |
var results = []; | |
// Filter color | |
if(color.match(keyword) != null) { | |
// Match keyword | |
var filtered = color.match(keyword); | |
// Get original content | |
var matchKeyword = filtered.input; | |
if(color == matchKeyword) { | |
results.push(value); | |
} | |
} | |
// Filter capacity | |
if(capacity.match(keyword) != null) { | |
// Match keyword | |
var filtered = capacity.match(keyword); | |
// Get original content | |
var matchKeyword = filtered.input; | |
if(capacity == matchKeyword) { | |
results.push(value); | |
} | |
} | |
// return | |
if(results.length == 1) { | |
return results; | |
} | |
} | |
} | |
} | |
// Run function in need, pass parameter you need. | |
// @example: searching by keyword : Grey | |
timApp.filterData.filterBySearchTerm('Grey'); | |
// Resulsts | |
// => [ { id: 5, | |
// name: 'iPhone SE', | |
// color: 'Space Grey', | |
// type: 'Wifi', | |
// capacity: '16GB', | |
// price: 629 }, | |
// { id: 6, | |
// name: 'iPhone SE', | |
// color: 'Space Grey', | |
// type: 'Wifi', | |
// capacity: '64GB', | |
// price: 829 } ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment