I am so new on typescript/javascript that I would like to modify each element of array with some function like following code. but I am wondering if there is a more concise/clear way to build my expected array from variable?
const variable = [1,2,3,4,5,6]
function add(a: number, b: number): number {
return a + b;
}
console.log(variable.map((i: number) => {return add(100, i)})) # =>[ 101, 102, 103, 104, 105, 106 ]
In ES6, if you have single expression as a return statement then you can simply re-write it as:
Also, if you declare
variableas an array ofnumberlike:Then inside
.map()method you will not need to declarei: numberas that would be automatically inferred. So, the.map()logic will become even more concise like:Or, you can simply do it without using the
add()function here: