Skip to content

Instantly share code, notes, and snippets.

@freewayz
Created January 4, 2016 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freewayz/56bd9db6d4164a42be75 to your computer and use it in GitHub Desktop.
Save freewayz/56bd9db6d4164a42be75 to your computer and use it in GitHub Desktop.
//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