Reputation: 35
hello to every one i hope you have a good day :)
i need to define a function, It should accept an array of strings which may contain additional space characters.
the function should use the array map method in order to make a new array full of trimmed names.
i tried something like that but its not close to work unfortunately
function cleanNames(array) {
const trimArray = array.map() function() {
return array.trim();
}
}
Upvotes: 0
Views: 870
Reputation: 68943
Your code throws the following syntax error:
Uncaught SyntaxError: Unexpected token 'function'
in array.map() function() {
You have to trim the names inside the map
callback function. You can also use arrow function (=>
) syntax to shorten the code:
function cleanNames(arr) {
return arr.map(i => i.trim());
}
console.log(cleanNames(['John ', ' Jane', ' Joe ']));
Upvotes: 2
Reputation: 127
You should try and make the minimum amount of code:
[' aa ', ' b b ', ' c c '].map(i=>i.trim());
This will solve your problem.
By the way, if you are using jQuery the syntax is a bit different and even simpler:
$.map([' aa ', ' bb ', ' cc '], $.trim);
Upvotes: 0
Reputation: 6746
function cleanNames(input) {
return input.map(val => val.trim());
}
const names = [" Joy ", " James", "Raj"];
console.log(cleanNames(names));
Upvotes: 1
Reputation: 18951
console.log([" Mike", " Hellen", "Robert "].map(name => name.trim()));
Upvotes: 1
Reputation:
You should do it like this:
var array = [" Mike", " Hellen", "Robert "];
array = array.map(function (el) {
return el.trim();
});
console.log(array);
you have to do the edit in map function.
Upvotes: 1