简体   繁体   中英

How to create dynamic lists from a json in javascript?

I am retrieving data with the following structure from a REST API

job_execs = 
[{
    "build_id": 12,
    "job": {
      "name": "test_job"
    },
    "product": {
      "name": "new_product"
    },
    "time_start": "2017-08-29T01:01:19.314000-07:00",
    "time_end": "2017-08-29T01:17:07.990000-07:00",
    "status": {
      "name": "SUCCESS"
    },
    "stage_executions": [{
        "stage": {
          "name": "stage-checkout"
        },
        "status": {
          "name": "SUCCESS"
        },
        "duration_millis": 119,
        "time_start": "2017-08-29T01:16:43.901000-07:00"
      },
      {
        "stage": {
          "name": "stage-wiki"
        },
        "status": {
          "name": "SUCCESS"
        },
        "duration_millis": 14225,
        "time_start": "2017-08-29T01:16:29.599000-07:00"
      },
      {
        "stage": {
          "name": "stage-upload"
        },
        "status": {
          "name": "SUCCESS"
        },
        "duration_millis": 14225,
        "time_start": "2017-08-29T01:16:29.599000-07:00"
      }
    ]
  },
  {
    "build_id": 13,
    "job": {
      "name": "test_job"
    },
    "product": {
      "name": "new_product"
    },
    "time_start": "2017-08-29T01:01:19.314000-07:00",
    "time_end": "2017-08-29T01:17:07.990000-07:00",
    "status": {
      "name": "SUCCESS"
    },
    "stage_executions": [{
        "stage": {
          "name": "stage-checkout"
        },
        "status": {
          "name": "SUCCESS"
        },
        "duration_millis": 400,
        "time_start": "2017-08-29T01:16:43.901000-07:00"
      },
      {
        "stage": {
          "name": "stage-wiki"
        },
        "status": {
          "name": "SUCCESS"
        },
        "duration_millis": 34,
        "time_start": "2017-08-29T01:16:29.599000-07:00"
      },
      {
        "stage": {
          "name": "stage-upload"
        },
        "status": {
          "name": "SUCCESS"
        },
        "duration_millis": 250,
        "time_start": "2017-08-29T01:16:29.599000-07:00"
      }
    ]
  }
]

What I'm trying to do is drill into the stage executions per job and generate a list per stage name. I want to loop through every job and append the duration_millis to the appropriate stage list.

So taking the JSON I posted above, I would want the following lists created:

['stage_checkout', 119, 400]
['stage_wiki', 14225, 34]
['stage_upload', 1215, 250]

How do I accomplish this?

Any pointers or even a starting point would help.

This might work, I have broken it down to simple functions so you can understand what is happening here.

 var job_execs = [{ "build_id": 12, "job": { "name": "test_job" }, "product": { "name": "new_product" }, "time_start": "2017-08-29T01:01:19.314000-07:00", "time_end": "2017-08-29T01:17:07.990000-07:00", "status": { "name": "SUCCESS" }, "stage_executions": [{ "stage": { "name": "stage-checkout" }, "status": { "name": "SUCCESS" }, "duration_millis": 119, "time_start": "2017-08-29T01:16:43.901000-07:00" }, { "stage": { "name": "stage-wiki" }, "status": { "name": "SUCCESS" }, "duration_millis": 14225, "time_start": "2017-08-29T01:16:29.599000-07:00" }, { "stage": { "name": "stage-upload" }, "status": { "name": "SUCCESS" }, "duration_millis": 14225, "time_start": "2017-08-29T01:16:29.599000-07:00" } ] }, { "build_id": 13, "job": { "name": "test_job" }, "product": { "name": "new_product" }, "time_start": "2017-08-29T01:01:19.314000-07:00", "time_end": "2017-08-29T01:17:07.990000-07:00", "status": { "name": "SUCCESS" }, "stage_executions": [{ "stage": { "name": "stage-checkout" }, "status": { "name": "SUCCESS" }, "duration_millis": 400, "time_start": "2017-08-29T01:16:43.901000-07:00" }, { "stage": { "name": "stage-wiki" }, "status": { "name": "SUCCESS" }, "duration_millis": 34, "time_start": "2017-08-29T01:16:29.599000-07:00" }, { "stage": { "name": "stage-upload" }, "status": { "name": "SUCCESS" }, "duration_millis": 250, "time_start": "2017-08-29T01:16:29.599000-07:00" } ] } ] const func = (arr) => { var ch = {}; arr.map(ar => { ar.stage_executions.map((s) => { ch[s.stage.name] = []; }) }) return ch; } const func2 = (arr) => { var all = func(arr); for(var k in all) { arr.map((ar) => { ar.stage_executions.map((st) => { if (st.stage.name === k) { all[k].push(st.stage.name, st.duration_millis) } }) }) } return all; } const func3 = (arr) => { const all = func2(arr); for(var key in all) { all[key] = [...new Set(all[key])] } return all; } console.log(func3(job_execs))

This code sample might help you. As well i changed the invalid JSON to a valid one and hope this is still matching your data basis.

 job_execs = [{"build_id":12,"job":{"name":"test_job"},"product":{"name":"new_product"},"time_start":"2017-08-29T01:01:19.314000-07:00","time_end":"2017-08-29T01:17:07.990000-07:00","status":{"name":"SUCCESS"},"stage_executions":[{"stage":{"name":"stage-checkout"},"status":{"name":"SUCCESS"},"duration_millis":119,"time_start":"2017-08-29T01:16:43.901000-07:00"},{"stage":{"name":"stage-wiki"},"status":{"name":"SUCCESS"},"duration_millis":14225,"time_start":"2017-08-29T01:16:29.599000-07:00"},{"stage":{"name":"stage-upload"},"status":{"name":"SUCCESS"},"duration_millis":14225,"time_start":"2017-08-29T01:16:29.599000-07:00"}]},{"build_id":13,"job":{"name":"test_job"},"product":{"name":"new_product"},"time_start":"2017-08-29T01:01:19.314000-07:00","time_end":"2017-08-29T01:17:07.990000-07:00","status":{"name":"SUCCESS"},"stage_executions":[{"stage":{"name":"stage-checkout"},"status":{"name":"SUCCESS"},"duration_millis":400,"time_start":"2017-08-29T01:16:43.901000-07:00"},{"stage":{"name":"stage-wiki"},"status":{"name":"SUCCESS"},"duration_millis":34,"time_start":"2017-08-29T01:16:29.599000-07:00"},{"stage":{"name":"stage-upload"},"status":{"name":"SUCCESS"},"duration_millis":250,"time_start":"2017-08-29T01:16:29.599000-07:00"}]}] var aCheckout = ['stage-checkout']; var aWiki = ['stage-wiki']; var aUpload = ['stage-upload']; job_execs.forEach(function (build) { build.stage_executions.forEach(function (stage) { switch(stage.stage.name){ case "stage-checkout": aCheckout.push(stage.duration_millis); break; case "stage-wiki": aWiki.push(stage.duration_millis); break; case "stage-upload": aUpload.push(stage.duration_millis); break; } }); }) console.log(aCheckout, aWiki, aUpload);

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