首页 > 解决方案 > 我想将存储在 JS 文件中的 JSON 变量触发到 HTML 文件中并遍历内容

问题描述

我遇到了一个问题,我将一些 JSON 数据存储在函数中定义的变量中。因此,要访问 JSON 数据,我首先需要进行函数调用。我的功能如下:

    viewDeposits: function() {
        var deposits = [];
        App.contracts.BOKCoin.deployed().then(function(instance) {
            bokcoinInstance = instance;
            return bokcoinInstance.depositCount();
        }).then(async function(depositCount) {
            var count = depositCount.toNumber();
            console.log(count);
            for(var i=1; i<=count; i++) {
                var x = await bokcoinInstance.deposits(i);
                deposits.push(x);
            }
            console.log(deposits);

            //send deposits to an html as an id 
            $('#deposits').html(deposits);
            return deposits;
        });
    }

接下来,我想调用此函数并将其存储在 .HTML 文件中的变量中并循环遍历其内容。我能够将返回对象存储到变量中。

任何帮助将不胜感激。谢谢!

标签: javascriptjqueryjson

解决方案


推荐阅读