function shuffle(arr) {
for (let i = arr.length - 1; i > 0; --i) {
const pos = Math.floor(Math.random() * (i + 1));
const t = arr[pos];
arr[pos] = arr[i];
arr[i] = t;
}
return arr;
}
const newArr = shuffle(imagesData.slice()); // новый перемешанный, imagesData не поменялось
const newArr2 = shuffle(imagesData); // перемешали imagesData, присвоили в newArr2
const currentDate = moment(); // текущая дата
// далее к дате комментария добавляем 6 месяцев и сравниваем с текущей датой
const isAfterSixMonth = moment(date).add(6, 'months').isAfter(currentDate);
if(isAfterSixMonth) { // если дата комеентария + 6 месяцев > текущая дата
// выводим нужный формат
}
function merge(key, ...arrs) {
const getKey = key instanceof Function ? key : n => n[key];
const result = new Map;
arrs.forEach(arr => arr.forEach(n => {
const k = getKey(n);
result.set(k, Object.assign(result.get(k) ?? {}, n));
}));
return [...result.values()];
}
const result = merge('id', testResult, test);function merge(key, target, ...arrs) {
const getKey = key instanceof Function ? key : n => n[key];
const targetMap = new Map(target.map(n => [ getKey(n), n ]));
arrs.forEach(arr => arr.forEach(n => {
const k = getKey(n);
targetMap.has(k) && Object.assign(targetMap.get(k), n);
}));
return target;
}
merge(n => n.id, testResult, test);
function weightedRandom(arr, key = () => 1) {
const val = key instanceof Function ? key : n => n[key];
const max = arr.reduce((acc, n) => acc + val(n), 0);
return () => {
let rand = Math.random() * max;
return arr.find(n => (rand -= val(n)) < 0);
};
}const getRandomPhotosArray = weightedRandom([
[ 9, truePhotos ],
[ 1, funnyPhotos ],
], 0);
function getPhoto() {
const photos = getRandomPhotosArray()[1];
return photos[Math.random() * photos.length | 0];
}