How to trim all strings in an array with Node.js

2 Answers

0 votes
let arr = [' node.js   ', '', '', ' c   ', '', '   c++', '98', '', '      python '];
 
arr = arr.map(element => {
  if (typeof element === 'string') {
      return element.trim();
  }
 
  return element;
});
 
console.log(arr);
 
   
   
   
     
       
       
/*
run:
       
[ 'node.js', '', '', 'c', '', 'c++', '98', '', 'python' ]
       
*/

 



answered Jun 29, 2022 by avibootz
0 votes
let arr = [' node.js   ', '', '', ' c   ', '', '   c++', '98', '', '      python '];
 
arr = arr.map(s => s.trim());
 
console.log(arr);
 
   
   
   
     
       
       
/*
run:
       
[ 'node.js', '', '', 'c', '', 'c++', '98', '', 'python' ]
       
*/

 



answered Jun 29, 2022 by avibootz

Related questions

1 answer 98 views
98 views asked Nov 23, 2024 by avibootz
2 answers 143 views
143 views asked Jul 6, 2024 by avibootz
1 answer 98 views
1 answer 160 views
...