繁体   English   中英

从 javascript 数据集中删除一行并将其转移到另一个数据集中?

[英]Removing a row from a javascript dataset and carrying it over into another dataset?

本质上,我有 2 个数据集和一个 function。 (名称后面的数字是统计数据,只是占位符,但我需要它们准确地结转)

var randomIngredientsList = [["Bread", 1, 2], ["Water", 5, 2]]
var ingredients = [["Mustard", 2, 3]]

当我应该得到一种随机成分时,我想从randomIngredientsList ,删除该特定部分,然后将其放入ingredients中。 还会有不同的randomIngredientsList具有不同的层级和统计数据,所以我需要它来区分它们。

我的 function 现在看起来像这样:

function generateRandomIngredient(nameOfArray){ 
        var randomValue = Math.floor(Math.random() * nameOfArray.length) //grabs a random number from 0-the length of the dataset
        console.log(randomValue)
            if(nameOfArray.length = 0){     // if ingredient  list is empty
            exploreLogger.unshift("You seem to have picked clean all the ingredients in this tier. Good job!"); //explorelogger is a log at the end of the screen, which shows events
            } else { //if it's not empty
    ingredients.push(nameOfArray[randomValue]) // add it to the ingredients list
    nameOfArray.splice(randomValue, 1) //remove the row from the dataset
            }
}

当它使用generateRandomIngredient(randomIngredientsList)运行它时,它会删除randomIngredientsList中的所有对象并向ingredients添加一个未定义的值。 我不知道该怎么办,我在这个问题上花了 2 个小时,并没有在网上找到任何关于它的信息。 感谢任何提供任何帮助或答案的人。

您将0分配给数组的长度,而不是进行比较。

if(nameOfArray.length = 0){  

应该改为

if(nameOfArray.length === 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM