根据键中存储的概率在数组中选择随机对象

问题描述 投票:0回答:1

我正在学习 javascript,我个人的学习项目之一是角色扮演游戏。我已经尝试让它工作好几天了,使用我在 stackoverflow 上看到的函数并尝试针对我的特定情况编辑它们,但似乎没有任何效果。该游戏是基于文本的,现在我正在开发“战利品系统”,所以我正在寻找的是这样的:当单击“搜索”按钮时,会激活一个功能来查看给定数组中的对象,并根据概率选择一个对象。我为每个对象分配了一个名为 por: 的键,这就是我希望该对象出现的概率。

顺便说一句,对不起我的英语。

const itemOne = {name: "Numero uno", por: 0.20, info: "some info about the item"};
const itemTwo = {name: "Numero dos", por: 0.10, info: "some info about the item"};
const itemThree = {name: "Numero tres", por: 0.15, info: "some info about the item"};
const itemFour = {name: "Numero cuatro", por: 0.15, info: "some info about the item"};
const itemFive = {name: "Numero cinco", por: 0.25, info: "some info about the item"};
const itemSix = {name: "Numero seis", por: 0.10, info: "some info about the item"};
const itemSeven = {name: "Numero siete", por: 0.05, info: "some info about the item"};


let lootAvailable = [itemOne, itemTwo, ItemThree, ItemFour, ItemFive, ItemSix, ItemSeven];

下面是我从 stackoverflow 复制的最后一个函数。我已经尝试了几种,但无法使它们发挥作用。老实说,我并不完全理解这个函数是如何工作的,而且我还没有在我正在学习的课程中经历过对象或循环。但这个游戏对我来说很有趣,所以......也许我现在咀嚼的东西超出了我现在能咬的范围?

function search(lootAvailable) {
    var winner = Math.random();
    var threshold = 0;
    for (let i = 0; i < lootAvailable.length; i++) {
        threshold += parseFloat(lootAvailable[i].por);
        if (threshold > winner) {
            return lootAvailable[i];   
        }
    }
}

当我在游戏上尝试此操作时,在控制台中显示以下错误: juegorpg.js:54 Uncaught TypeError: 无法读取未定义的属性(读取“长度”)

所以。我想知道根据键的概率从数组列表中获取对象的最有效方法是什么。

再次抱歉我的英语水平和缺乏经验。

javascript arrays object key probability
1个回答
0
投票

您应该将

lootAvailable
作为参数传递:

const itemOne = {name: "Numero uno", por: 0.20, info: "some info about the item"};
const itemTwo = {name: "Numero dos", por: 0.10, info: "some info about the item"};
const itemThree = {name: "Numero tres", por: 0.15, info: "some info about the item"};
const itemFour = {name: "Numero cuatro", por: 0.15, info: "some info about the item"};
const itemFive = {name: "Numero cinco", por: 0.25, info: "some info about the item"};
const itemSix = {name: "Numero seis", por: 0.10, info: "some info about the item"};
const itemSeven = {name: "Numero siete", por: 0.05, info: "some info about the item"};


let lootAvailable = [itemOne, itemTwo, itemThree, itemFour, itemFive, itemSix, itemSeven];

function search(lootAvailable) {
    var winner = Math.random();
    var threshold = 0;
    for (let i = 0; i < lootAvailable.length; i++) {
        threshold += parseFloat(lootAvailable[i].por);
        if (threshold > winner) {
            return lootAvailable[i];   
        }
    }
}

console.log(search(lootAvailable))

或者直接删除它:

const itemOne = {name: "Numero uno", por: 0.20, info: "some info about the item"};
const itemTwo = {name: "Numero dos", por: 0.10, info: "some info about the item"};
const itemThree = {name: "Numero tres", por: 0.15, info: "some info about the item"};
const itemFour = {name: "Numero cuatro", por: 0.15, info: "some info about the item"};
const itemFive = {name: "Numero cinco", por: 0.25, info: "some info about the item"};
const itemSix = {name: "Numero seis", por: 0.10, info: "some info about the item"};
const itemSeven = {name: "Numero siete", por: 0.05, info: "some info about the item"};


let lootAvailable = [itemOne, itemTwo, itemThree, itemFour, itemFive, itemSix, itemSeven];

function search() {
    var winner = Math.random();
    var threshold = 0;
    for (let i = 0; i < lootAvailable.length; i++) {
        threshold += parseFloat(lootAvailable[i].por);
        if (threshold > winner) {
            return lootAvailable[i];   
        }
    }
}

console.log(search(lootAvailable))

© www.soinside.com 2019 - 2024. All rights reserved.