NodeJs Javascript从数组动态调用函数

时间:2018-07-23 08:11:40

标签: javascript node.js

因此,我一直在研究堆栈溢出问题,似乎找不到人们认为可行的任何东西。

所以我有一个对象数组

Report_Search_List = [
    {"NAME":"CHART OF ACCOUNTS", "PDF":"CHART_OF_ACCOUNTS_PDF", "XLS":"CHART_OF_ACCOUNTS_XLS"},
    {"NAME":"GENERAL LEDGER", "PDF":"GENERAL_LEDGER_PDF", "XLS":"GENERAL_LEDGER_XLS"},
    {"NAME":"COST REPORT", "PDF":"COST_REPORT_PDF", "XLS":"COST_REPORT_XLS"},
    {"NAME":"CASH FLOW", "PDF":"CASH_FLOW_PDF", "XLS":"CASH_FLOW_XLS"},
    {"NAME":"INVOICE", "PDF":"INVOICE_PDF", "XLS":"INVOICE_XLS"},
    {"NAME":"CREDIT NOTE", "PDF":"CREDIT_NOTE_PDF", "XLS":"CREDIT_NOTE_XLS"},
    {"NAME":"JOBCARD COST", "PDF":"JOBCARD_COST_PDF", "XLS":"JOBCARD_COST_XLS"},
    {"NAME":"GOODS RECEIVED VOUCHER", "PDF":"GOODS_RECEIVED_VOUCHER_PDF", "XLS":"GOODS RECEIVED VOUCHER_XLS"},
    {"NAME":"GOODS RETURNED NOTE", "PDF":"GOODS_RETURNED_NOTE_PDF", "XLS":"GOODS_RETURNED_NOTE_XLS"},
    {"NAME":"REQUISITION", "PDF":"REQUISITION_PDF", "XLS":"REQUISITION_XLS"},
    {"NAME":"DELIVERY NOTE", "PDF":"DELIVERY_NOTE_PDF", "XLS":"DELIVERY_NOTE_XLS"},
    {"NAME":"PICK SLIP", "PDF":"PICK_SLIP_PDF", "XLS":"PICK_SLIP_XLS"},
    {"NAME":"PETTY CASH", "PDF":"PETTY_CASH_PDF", "XLS":"DELIVERY_NOTE_XLS"},
    {"NAME":"OTHER TRANSACTIONS", "PDF":"OTHER_TRANSACTIONS_PDF", "XLS":"OTHER_TRANSACTIONS_XLS"},
    {"NAME":"PURCHASE ORDER", "PDF":"PURCHASE_ORDER_PDF", "XLS":"PURCHASE_ORDER_XLS"}
]

因此,它首先会建立一个搜索列表供用户选择

Report_Search_List[index].NAME

然后,他们选择了报告后,单击PDF或XLS按钮

这是功能

function PDF_CLICK() {
    try {
        var text = "none";
        var BRANCH_PDF_REPORTS_PROFILE_SELECT = document.getElementById('BRANCH_PDF_REPORTS_PROFILE_SELECT');
        text = BRANCH_PDF_REPORTS_PROFILE_SELECT.value;
        //get array list
        var report = Report_Search_List.filter(function (el) {
            return el["NAME"] == text;
        });
        //PROBLEM IS HERE
//run method
Window[report[0]["PDF"]]();
window[report[0]["PDF"]]();
report[0]["PDF"].call();
report[0]["PDF"]();
    } catch (e) {
        console.log(e);
        EX_JS_ALERT.ALERT("OOPS SOMETHING WENT WRONG"); EX_JS_ERROR.ERROR(e.toString(), location.pathname);;
    }
}

现在是问题

我已经尝试了以上所有方法,并且它获取了正确的报告,但仍然返回其没有功能。同时下面有一个功能。

2 个答案:

答案 0 :(得分:1)

这被标记为node.js,因此我认为它不在浏览器中运行。因此,没有window对象可用于查找全局变量。您可以在Node的global对象上显式定义事物,但这不是一个好主意。更好的方法是创建一个对象,该对象包含带有要用调用它们的文本命名的键的函数。

因此,对于您的数据子集,您可能会进行如下安排:

const Report_Search_List = [
    {"NAME":"CHART OF ACCOUNTS", "PDF":"CHART_OF_ACCOUNTS_PDF", "XLS":"CHART_OF_ACCOUNTS_XLS"},
    {"NAME":"GENERAL LEDGER", "PDF":"GENERAL_LEDGER_PDF", "XLS":"GENERAL_LEDGER_XLS"}
]
    
const functions = {
  CHART_OF_ACCOUNTS_PDF(){console.log("chart called")},
  GENERAL_LEDGER_PDF(){console.log("Ledger call")}
  
}

// now you can call them with strings:
functions[Report_Search_List[0]["PDF"]]()
functions[Report_Search_List[1]["PDF"]]()

答案 1 :(得分:0)

所以我想出了怎么做窗口不是您正在使用的当前窗口。

所以我在页面顶部所做的是

const EX_JS_BRANCH_REPORTS_PDF = require('../BRANCH/EX_JS_BRANCH_REPORTS_PDF.js');

然后更改了

功能

exports.GENERAL_LEDGER_PDF = function(){}

然后我将窗口更改为EX_JS_BRANCH_REPORTS_PDF并动态运行XD

EX_JS_BRANCH_REPORTS_PDF[report[0]["PDF"]]();