[Solved] How to create dynamic lists from a json in javascript?


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

2

solved How to create dynamic lists from a json in javascript?