Created
January 4, 2016 18:37
-
-
Save freewayz/56bd9db6d4164a42be75 to your computer and use it in GitHub Desktop.
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
//simple work arroud on getting a property type using a matcher from an array | |
//welcome any comment and modification / enhacnement | |
var myArray = [{"name" : "pitaside", "id" : 1}, {"name":"github", "id" : 3}] | |
filterArrayByType: function (arrayToMatch, fieldType, matcher) { | |
if(! arrayToMatch instanceof Array){throw ("Not an Array")} | |
var filterTypeToReturn = arrayToMatch.filter((items) => { | |
var temp; | |
if (items[String(fieldType)] === matcher) { | |
temp = items[String(fieldType)] | |
} | |
return temp; | |
} | |
); | |
return filterTypeToReturn; | |
} | |
var myMatcher = 'github' | |
var id3 = filterArrayByType(myArray, 'name', myMatcher)[0].id | |
//returns 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment