0

My Array of object is below

 var filterStatus = [{ doc_count: 49, key: "Completed", color: "#1DC9B7" }, { doc_count: 147, key: "Failed", color: "#F14C69" }, { doc_count: 321, key: "In Progress", color: "#FFC568" }, { doc_count: 29, key: "Started" }];

I want to get result which contains Completed, Failed and In progress (Should Contains Started) status

I am trying below code

var result = filterStatus.filter(obj => { if(obj.key == 'Started' || obj.key == 'In Progress'){ return obj}} ).map(obj => obj.doc_count).reduce((p, c) => p + c, 0);

and i am getting result 350

My expected output is

[{ doc_count: 49, key: "Completed", color: "#1DC9B7" }, { doc_count: 147, key: "Failed", color: "#F14C69" }, { doc_count: 350, key: "In Progress", color: "#FFC568" }];

Note: Expected output contains addition of Started and In progress inside In Progress doc_count.

1
  • 1
    reduce() turns an array into a single value. Commented Jul 31, 2019 at 9:41

3 Answers 3

1

const filterStatus = [{ doc_count: 49, key: "Completed", color: "#1DC9B7" }, { doc_count: 147, key: "Failed", color: "#F14C69" }, { doc_count: 321, key: "In Progress", color: "#FFC568" }, { doc_count: 29, key: "Started" }];
// filter out "Started"
const result = filterStatus.filter(({key}) => key !== "Started");
// find and update "In Progress"
result.find(({key}) => key === "In Progress").doc_count += filterStatus.find(({key}) => key === "Started").doc_count;
console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

1

Use reduce and map seperately

var filterStatus = [{
  doc_count: 49,
  key: "Completed",
  color: "#1DC9B7"
}, {
  doc_count: 147,
  key: "Failed",
  color: "#F14C69"
}, {
  doc_count: 321,
  key: "In Progress",
  color: "#FFC568"
}, {
  doc_count: 29,
  key: "Started"
}];
 var val=filterStatus.map(function(e){
  if(e.key=="In Progress" || e.key=="Started")
  return e.doc_count
  else
  return 0}).reduce(function(acc,e){return acc+=e},0)
var result = filterStatus.filter(function(obj) {
if(obj.key == 'In Progress')
obj.doc_count=val;
      if (obj.key == 'Completed' || obj.key == 'In Progress' || obj.key == 'Failed')
        return obj;
    })
   console.log(result)
 

2 Comments

Result is different from what OP expected
Still different from expectations
-1
var filterStatus = [{ doc_count: 49, key: "Completed", color: "#1DC9B7" }, { doc_count: 147, key: "Failed", color: "#F14C69" }, { doc_count: 321, key: "In Progress", color: "#FFC568" }, { doc_count: 29, key: "Started" }];


function mergeInProgressStarted () {
 return filterStatus.filter(({key}) => {
   return ['In Progress', 'Started'].indexOf(key) > -1;
}).reduce((acc, status) => {
  acc.doc_count += status.doc_count;
  if (status.color) {
    acc.color = status.color;
  } 
  return acc;
  }, { key: "In Progress", doc_count: 0 })
}



filterStatus.filter(({ key  }) => ['Completed', 'Failed'].indexOf(key) > -1).concat([mergeInProgressStarted()])

1 Comment

Result is different from what OP expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.