strip each element in list in python in javascript

In Python, you can use a list comprehension with the strip() method to strip each element in a list:

main.py
my_list = ["  hello ", "  world  "]
stripped_list = [elem.strip() for elem in my_list]
print(stripped_list)  # Output: ['hello', 'world']
138 chars
4 lines

In JavaScript, you can use the map() method to apply the trim() method to each element in an array:

index.tsx
let myArray = ["  hello ", "  world  "];
let trimmedArray = myArray.map(elem => elem.trim());
console.log(trimmedArray);  // Output: ['hello', 'world']
152 chars
4 lines

Note that trim() removes whitespace from the beginning and end of a string in JavaScript, while strip() removes both leading and trailing spaces, tabs and newlines from a string in Python.

related categories

gistlibby LogSnag