avocado-old/packages/core/array.js

32 lines
646 B
JavaScript
Raw Normal View History

2019-10-03 16:39:27 -05:00
export function arrayUnique(array) {
return Array.from((new Set(array)).values());
}
export function flatten(array) {
2020-06-19 15:29:21 -05:00
return [].concat.apply([], array)
2019-10-03 16:39:27 -05:00
}
2019-11-20 00:53:35 -06:00
export function fromObject(object) {
const array = [];
for (const key in object) {
array.push(object[key]);
}
return array;
}
2020-05-21 08:55:07 -05:00
2020-05-26 03:20:34 -05:00
export function shuffle(array) {
const shuffled = [...array];
shuffleInPlace(shuffled);
return shuffled;
}
2020-05-21 08:55:07 -05:00
export function shuffleInPlace(array) {
let i = 0, j = 0, temp = null;
for (i = array.length - 1; i > 0; i -= 1) {
j = Math.floor(Math.random() * (i + 1))
temp = array[i]
array[i] = array[j]
array[j] = temp
}
}