记录与数组相关的方法,持续更新…
判断数组中有没有某个元素
indexOf
events: string[] = ['start', 'move', 'end']
if (events.indexOf('start')!== -1) {
...
}
需要重复操作类似的方法,可以将其放进一个数组内,然后通过 forEach 调用
const stateArr = [{
type: getSongList,
cb: list => this.watchList(list, 'songList')
}, {
type: getPlayList,
cb: list => this.watchList(list, 'playList')
}, {
type: getCurrentIndex,
cb: index => this.watchCurrentIndex(index)
}];
stateArr.forEach(item => {
appStore$.pipe(select(item.type)).subscribe(item.cb);
})
随机打乱数组中的元素排序
function getRandomInt(range: [number, number]): number {
return Math.floor(Math.random() * (range[1] - range[0] + 1) + range[0]);
}
function shuffle<T>(arr: T[]): T[] {
const result = arr.slice();
for (let i = 0; i < result.length; i++) {
const j = getRandomInt([0, i]);
[result[i], result[j]] = [result[j], result[i]];
}
return result;
}