ecmascript 6 - how to remove character from array using filters and map? -
i trying remove specific characters array. passing sentence,characters sentence should removed alpha array using map() , filters(),
var alpha =['b','c','d','e','f','g','h','a'] function removealpha(sentence){ return alpha.map(function(melem,mpos,marr){ return sentence.tolowercase().split("").filter(function(elem,pos,arr){ melem!=elem }); }); } console.log(removealpha('bdog')); please let me know ,what doing wrong
the inner callback function not return value. melem!=elem should return melem!=elem
after correction inner filter returns array 1 alpha letter removed it, letter. in next iteration of outer map, start scratch , return array second alpha letter removed, etc... gives array of arrays, in each array 1 of alpha characters removed.
yet, need different: want characters alpha array not in sentence (instead of characters of sentence not in alpha array).
for should apply filter on alpha:
var alpha =['b','c','d','e','f','g','h','a'] function removealpha(sentence){ return alpha.filter(function(melem){ return !this.includes(melem); }, sentence.tolowercase()); } console.log(removealpha('bdog'));
Comments
Post a Comment