lucky-item.js
/**
     * Randomly select some indexs from the array based on weights.
     *
     * @param {object[]} arr The array for selection.
     * @param {string | Function} weights The field name of weights or a function that returns weights.
     * @param {number} count The number of indexs you want to select.
     * @param {object} options
     * @returns Returns the array of lucky indexs.
     */
Randomly select some indexs from the array based on weights.
@param {object[]} arr The array for selection.
@param {string | Function} weights The field name of weights or a function that returns weights.
@param {number} count The number of indexs you want to select.
@param {object} options
@returns Returns the array of lucky indexs.
Randomly select some indexs from the array based on weights . @param { object [] } arr The array for selection . @param { string | Function } weights The field name of weights or a function that returns weights . @param { number } count The number of indexs you want to select . @param { object } options @returns Returns the array of lucky indexs .
indexsBy(arr, weights, count, options = {}) {
        const unique = options.unique === undefined ? this.unique : options.unique;
        const random = options.random || this.random;
        count = unique && arr.length < count ? arr.length : count;
        const weightsFunc = typeof weights === 'string'
            ? (o) => o[weights]
            : weights;
        const weightsArr = arr.map((a) => weightsFunc(a));
        let totalWeights = weightsArr.reduce((acc, cur) => acc + cur, 0);
        const arrWithIndex = weightsArr.map((value, index) => [index, value]);
        return [...new Array(count)].map(() => {
            const randomInt = random(1, totalWeights);
            let startWeight = 0;
            const find = arrWithIndex.find((a) => {
                startWeight += a[1];
                return startWeight >= randomInt;
            });
            if (unique) {
                totalWeights -= find[1];
                arrWithIndex[find[0]][1] = 0;
            }
            return find[0];
        });
    }
indexsBy ( arr , weights , count , options = { } ) { const unique = options . unique === undefined ? this . unique : options . unique ; const random = options . random || this . random ; count = unique && arr . length < count ? arr . length : count ; const weightsFunc = typeof weights === 'string' ? ( o ) => o [ weights ] : weights ; const weightsArr = arr . map ( ( a ) => weightsFunc ( a ) ) ; let totalWeights = weightsArr . reduce ( ( acc , cur ) => acc + cur , 0 ) ; const arrWithIndex = weightsArr . map ( ( value , index ) => [ index , value ] ) ; return [ ... new Array ( count ) ] . map ( ( ) => { const randomInt = random ( 1 , totalWeights ) ; let startWeight = 0 ; const find = arrWithIndex . find ( ( a ) => { startWeight += a [ 1 ] ; return startWeight >= randomInt ; } ) ; if ( unique ) { totalWeights -= find [ 1 ] ; arrWithIndex [ find [ 0 ] ] [ 1 ] = 0 ; } return find [ 0 ] ; } ) ; }
Randomly select some indexs from the array based on weights.
Randomly select some indexs from the array based on weights .