December 2016
Intermediate to advanced
841 pages
17h
English
Since we will work a lot with arrays, we can use some utility functions. First, we create a function that flattens a two dimensional array into a one dimensional array:
export function flatten<U>(array: U[][]) {
return (<U[]>[]).concat(...array);
}
Next, we create a function that replaces a single element of an array with a specified value. We will use functional programming in this chapter again, so we must use immutable data structures. We can use map for this, since this function provides both the element and the index to the callback. With this index, we can determine whether that element should be replaced:
export function arrayModify<U>(array: U[], index: number, newValue: U) { return array.map((oldValue, currentIndex) ...Read now
Unlock full access