![](/img/trans.png)
[英]how to create a counter that collects values from an array to pass as a parameter?
[英]Pass array values as parameter to function and create json data
我有一个场景,我将一个对象数组传递给 nodejs 中的 function,但同样失败并出现未定义的错误。
这是我尝试过的:
var object = issues.issues //json data
var outarr=[];
for(var key in object){
outarr.push(object[key].key)
}
console.log(outarr) // array is formed like this : ['a','b','c','d','e']
for(var i =0; i<outarr.length;i++){
jira.findIssue(outarr[i]) //here I am trying to pass the array objects into the loop one by one
.then(function(issue) {
var issue_number = issue.key
var ape = issue.fields.customfield_11442[0].value
var description = issue.fields.summary
var ice = issue.fields.customfield_15890[0].value
var vice = issue.fields.customfield_15891.value
var sor = issue.fields.labels
if (sor.indexOf("testcng") > -1) {
var val = 'yes'
} else {
var val = 'yes'
}
var obj = {};
obj['ape_n'] = ape;
obj['description_n'] = description;
obj['ice_n'] = ice;
obj['vice_n'] = vice;
obj['sor_n'] = val;
var out = {}
var key = item;
out[key] = [];
out[key].push(obj);
console.log(out)
} })
.catch(function(err) {
console.error(err);
});
});
我想要实现的目标:我想将数组值作为jira.findissue
所需的参数(基本上传递问题编号)一个一个地传递,并且应该再次获取这些值并给出组合 json output。 如何在此 function 中一一传递此数组值,并在循环中运行jira.findissue
。
任何帮助都会很棒:! :-)
我已经查看了您问题中的代码。 老实说,您编写的代码很混乱,并且包含一些简单的语法错误。
一个好的提示是使用 linter 来避免这些错误。 更多关于 linters 的信息: https://www.codereadability.com/what-are-javascript-linters/
对于 output 所有结果都在一个数组中,您必须在循环的 scope 之外定义数组。
我稍微清理了代码并使用了一些 es6 功能。 我不知道代码的上下文,但这是我可以做到的:
//map every value the key to outarr
let outarr = issues.issues.map( elm => elm.key);
//Output defined outside the scope of the loop
let output = [];
//looping outarr
outarr.forEach( el => {
jira.findIssue(el).then(issue => {
//creating the issue object
let obj = {
ape_n: issue.fields.customfield_11442[0].value,
description_n: issue.fields.summary,
ice_n: issue.fields.customfield_15890[0].value,
vice_n: issue.fields.customfield_15891.value,
sor_n: issue.fields.labels.indexOf("testcng") > -1 ? "yes" : "yes",
};
//pushing to the output
output[issue.key] = obj;
}).catch(err => {
console.log(err);
});
});
//ouputing the output
console.log(output);
有关 es6 功能的更多信息: https://webapplog.com/es6/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.