javascript - 如何使本地推送数组全局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 循环数组。
解决方案
推荐阅读
- r - 将 %incMSE 转换为 %incRMSE 有意义吗?
- graphql - 使用 Apollo 使用 @client 指令混合本地和远程字段
- python - 如果值不是升序python,则删除列
- angular - 如何检测自定义表单控件中绑定表单变量值的变化(Angular 10)
- node.js - 将csv数据直接写入节点js中的azure blob
- reactjs - 如何设置 MuiPhone 号码和 onChange 事件的值?
- python - Discord.py- ping 时如何将 bot 消息设置为服务器前缀?
- amazon-web-services - AWS S3 中的最后修改日期错误
- javascript - 如何在滚动时仅滚动鼠标指针内容?
- python - 如何将每行的列表元素插入 Python DataFrame