首页 > 解决方案 > 如何使本地推送数组全局javascript

问题描述

我最初在全局范围内将数组声明为空数组,然后在函数回调中声明。我必须将响应消息元素推送到这个数组。

但我需要在函数回调之外获取这个更新数组

 var array = []
 frappe.call({
        method: "frappe.client.get_list",
        args: {
            doctype: 'Attendance',

       filters: [['attendance_date', '>=', values.from_date],
                ['attendance_date', '<=', values.to_date]

            ]
        },
        "callback": function (response) {
            console.log(response.message)
            for (var i in response.message) {
                frappe.call({
                    method: "frappe.client.get_value",
                    args: {
                        doctype: 'Attendance',
                        fieldname: ['branch'],
                    filters: { 'name': ['=', response.message[i].name] }

                    },
                    "callback": function (data) {
                        frappe.call({
                            method: "frappe.client.get_value",
                            args: {
                                doctype: 'Medical Crew Salaries',
                                fieldname: ['total'],
                             filters: { 'cost_center': ['=',data.message.branch]}
                            },
                             "callback": function (r) {
                                    array.push({ branch: data.message.branch, total: r.message.total })

                            }
                        })
                    }
                })
            }
    }
    })
console.log(array.length)
var result=[]
array.reduce(function(res, value) {
if (!res[value.branch]) {
    res[value.branch] = { branch: data.message.branch, total: 0 };
    result.push(res[value.branch])
  }
  res[value.branch].total += r.message.total;
  return res;
}, {});
console.log(result)
frappe.model.with_doctype('Journal Entry', function() {
        var journal_entry = frappe.model.get_new_doc('Journal Entry');
        for(var c in result){
                var journalentry_account= frappe.model.add_child(journal_entry, 'accounts');
                    journalentry_account.debit_in_account_currency=result[c].total
                    journalentry_account.cost_center=result[c].branch
            
        }
                frappe.set_route('Form', 'Journal Entry', journal_entry.name);      
                        })
    


我需要这样做的原因是我需要在外面循环这个数组,第一个 for 循环数组。

标签: javascriptarrayserpnextfrappe

解决方案


推荐阅读