function to get percentage for progress bar with javascript - Stack Overflow

I have this array:[{"id": "5b221edea8abbc4194a62b90","name": "Job on

I have this array:

[{
    "id": "5b221edea8abbc4194a62b90",
    "name": "Job one",
    "tasks": [{
        "id": "5b221edea8abbc4194a62b91",
        "name": "Task 2",
        "status": "finished",
      },
      {
        "id": "5b221edea8abbc4194a62b91",
        "name": "Task 3",
        "status": "pending",
      },
      {
        "id": "5b221edea8abbc4194a62b91",
        "name": "Task 3",
        "status": "to_do",
      },
    ]
  },
  {
    "id": "5b221edea8abbc4194a62b91",
    "name": "Job two",
    "tasks": [{
        "id": "5b221edea8abbc4194a62b96",
        "name": "Task 5",
        "status": "finished",
      },
      {
        "id": "5b221edea8abbc4194a62b97",
        "name": "Task 8",
        "status": "accepted",
      },
    ]
  },
]

All tasks is the 100% of progress bar. There are 5 tasks in this case. 100% = 5 tasks.

I want to know what percentage the progress bar has, according to whether the task has status "finished" or not. In this case should be (100 x 2):5 = 40% pleted in progress bar.

Thank you!

I have this array:

[{
    "id": "5b221edea8abbc4194a62b90",
    "name": "Job one",
    "tasks": [{
        "id": "5b221edea8abbc4194a62b91",
        "name": "Task 2",
        "status": "finished",
      },
      {
        "id": "5b221edea8abbc4194a62b91",
        "name": "Task 3",
        "status": "pending",
      },
      {
        "id": "5b221edea8abbc4194a62b91",
        "name": "Task 3",
        "status": "to_do",
      },
    ]
  },
  {
    "id": "5b221edea8abbc4194a62b91",
    "name": "Job two",
    "tasks": [{
        "id": "5b221edea8abbc4194a62b96",
        "name": "Task 5",
        "status": "finished",
      },
      {
        "id": "5b221edea8abbc4194a62b97",
        "name": "Task 8",
        "status": "accepted",
      },
    ]
  },
]

All tasks is the 100% of progress bar. There are 5 tasks in this case. 100% = 5 tasks.

I want to know what percentage the progress bar has, according to whether the task has status "finished" or not. In this case should be (100 x 2):5 = 40% pleted in progress bar.

Thank you!

Share Improve this question edited Jun 14, 2018 at 8:28 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Jun 14, 2018 at 8:24 tomatitotomatito 4111 gold badge6 silver badges17 bronze badges 5
  • Please visit the help center, take the tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output. – mplungjan Commented Jun 14, 2018 at 8:27
  • It's Array of Objects - not a JSON – Arkej Commented Jun 14, 2018 at 8:27
  • We don't just implement stuff for you. We provide guidance in case you are stuck. It doesn't seem like you are stuck - it just seems there was no attempt made in the direction you are looking for. Please read about How to create a Minimal, Complete, and Verifiable example. Thank you! – Adelin Commented Jun 14, 2018 at 8:28
  • @NikhilAggarwal There are 2 of 5 pleted.. – Keith Commented Jun 14, 2018 at 8:30
  • @Arkej Appart from the extra mas at the end of some lines it's valid,.. I assume he's copy / pasted, and left out personal info, but left the ma's in by mistake. – Keith Commented Jun 14, 2018 at 8:33
Add a ment  | 

2 Answers 2

Reset to default 3

You can flatten the array using concat and map. Use reduce to get the percetange.

let arr = [{"id":"5b221edea8abbc4194a62b90","name":"Job one","tasks":[{"id":"5b221edea8abbc4194a62b91","name":"Task 2","status":"finished"},{"id":"5b221edea8abbc4194a62b91","name":"Task 3","status":"pending"},{"id":"5b221edea8abbc4194a62b91","name":"Task 3","status":"to_do"}]},{"id":"5b221edea8abbc4194a62b91","name":"Job two","tasks":[{"id":"5b221edea8abbc4194a62b96","name":"Task 5","status":"finished"},{"id":"5b221edea8abbc4194a62b97","name":"Task 8","status":"accepted"}]}]

let result = [].concat(...arr.map(o => o.tasks)).reduce((c, {status}) => {
  if (status === "finished") c.finished++;
  c.total++;
  c.percentage = (c.finished / c.total) * 100;
  return c;
}, {finished: 0,total: 0,percentage: 0})

console.log(result);

You can simply use nested forEach for calculating the percentage.

Try the following:

var arr =  [{"id":"5b221edea8abbc4194a62b90","name":"Job one","tasks":[{"id":"5b221edea8abbc4194a62b91","name":"Task 2","status":"finished"},{"id":"5b221edea8abbc4194a62b91","name":"Task 3","status":"pending"},{"id":"5b221edea8abbc4194a62b91","name":"Task 3","status":"to_do"}]},{"id":"5b221edea8abbc4194a62b91","name":"Job two","tasks":[{"id":"5b221edea8abbc4194a62b96","name":"Task 5","status":"finished"},{"id":"5b221edea8abbc4194a62b97","name":"Task 8","status":"accepted"}]}];

var pletedCount = 0;
var total = 0;
arr.forEach((o)=>{
  o.tasks.forEach((task) => {
    if(task.status == "finished")
      pletedCount++;
     total++;
  });
});
var percentage  = (pletedCount/total)*100;
console.log(percentage+"%");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744254514a4565329.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信