Create an array of JavaScript objects from an exis

2019-08-23 03:48发布

问题:

my first time posting a question here, so please let me know if I'm missing any information that is needed. Updated to include desired output.

I'm working on a google app script (basically javascript) and am trying to pull objects from an array of objects and create a new array of objects. I'm using the google base functions for getRowData (these can be found at : https://developers.google.com/apps-script/guides/sheets) to create my initial array of objects. This gives me a row of data similar (A JIRA export if anyone is wondering, with information cut):

{summary=Internal - Fix PuppetFile Jenkins Jobs, progress=1.0, issueType=Story, resolution=Done, timeSpent=3600.0, key=XXXXX-646, watchers=0.0, remainingEstimate=0.0, numberOfComments=1.0, status=Resolved, assignee=XXXXXXXX}

When I run my function:

for (var i = 0; i < issueList.length; i++){

rankList[i] = [issueList[i].summary,issueList[i].storyPoints,issueList[i].epicLink,issueList[i].fixVersions];
}

I get:

[Internal - Fix PuppetFile Jenkins Jobs, 3.0, null, null]

But what I want is:

{summary=Internal - Fix PuppetFile Jenkins Jobs, storyPoints=1.0, epicLink=StoryName, fixVersions=Done}

I'm not getting the key for the value, and I don't understand how the objects are constructed quite well enough to get it to transfer over. I looked at some examples of manipulating the key/value pairs but when I tried it on my own I just got a bunch of undefined. Thank you for any help you can provide.

回答1:

What you want is probably something like this:

rankList = [];
for (var i = 0; i < issueList.length; i++) {
    issue = issueList[i];
    rankList.push({
        summary: issue.summary,
        storyPoints: issue.progress,
        epicLink: issue.IDONTKNOW,
        fixVersions: issue.resolution
    });
}

I don't know what field goes in epicLink, since it wasn't obvious from your example. And I was just guessing about the other fields. But this is the general structure, you just need to make all the correct correspondences.



回答2:

Use jquery each instead it's much easier to get the keys and value at the same time:

var myarray = [];

$.each(issueList,function(key,value){
  console.log(key);
  console.log(value);

  value.key = key;

  myarray.push(value);
});

console.log(myarray);