简体   繁体   中英

Pass array values as parameter to function and create json data

I have a scenario where I am passing an array of objects to a function in nodejs, but the same is failing with undefined error.

Here is what I have tried:

 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);
  });


});

What I am trying to achieve: I want to pass the array values as a parameter which is required by jira.findissue (bassically passing the issue number) one by one and which should again fetch the values and give a combine json output. How can I pass this array values one by one in this function and also run jira.findissue in loop.

Any help will be great:! :-)

I have taken a look at the code in your question. To be honest the code you wrote is messy and contains some simple syntax errors.

A good tip is to use a linter to avoid those mistakes. More info about linters here: https://www.codereadability.com/what-are-javascript-linters/

To output all results in one array you have to define the array outside the scope of the loop.

I cleaned the code a bit up and use some es6 features. I don't know the context of the code but this is what I can make off it:

//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);

Some more info about es6 features: https://webapplog.com/es6/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM