function getRandomArrayElements(arr, count) {
let shuffled = arr.slice(0)
let i = arr.length
let min = i - count
let temp
let index
while (i-- > min) {
index = Math.floor((i + 1) * Math.random())
temp = shuffled[index]
shuffled[index] = shuffled[i]
shuffled[i] = temp
}
return shuffled.slice(min)
}
let items = ['1', '2', '4', '5', '6', '7', '8', '9', '10']
console.log(getRandomArrayElements(items, 4))
function getArrObject(arr, id) {
let tempArr = [], newArr = []
for (let i = 0; i < arr.length; i++) {
if (tempArr.indexOf(arr[i].id) === -1) {
newArr.push({
id: arr[i].id,
list: [arr[i]]
})
tempArr.push(arr[i].id);
} else {
for (let j = 0; j < newArr.length; j++) {
if (newArr[j].id == arr[i].id) {
newArr[j].list.push(arr[i])
}
}
}
}
return newArr
}
let arr = [
{ id: 1, list: { id: 66 } },
{ id: 1, list: { id: 44 } },
{ id: 2, list: { id: 55 } },
{ id: 2, list: { id: 33 } },
{ id: 3, list: { id: 11 } },
{ id: 3, list: { id: 22 } }
]
console.log(getArrObject(arr, 'id'))
getArrObject(arr, 'id')= [
({
id: 1,
newlist: [
{ id: 1, list: { id: 66 } },
{ id: 1, list: { id: 44 } },
],
},
{
id: 2,
newlist: [
{ id: 2, list: { id: 55 } },
{ id: 2, list: { id: 33 } },
],
},
{
id: 3,
newlist: [
{ id: 3, list: { id: 11 } },
{ id: 3, list: { id: 22 } },
],
}),
]
function RandomNum(Min, Max) {
var Range = Max - Min
var Rand = Math.random()
if (Math.round(Rand * Range) == 0) {
return Min + 1
} else if (Math.round(Rand * Max) == Max) {
index++
return Max - 1
} else {
var num = Min + Math.round(Rand * Range) - 1
return num
}
}