0

I have a bunch of objects inside an array and I have to check status of each object and return a single value for each array.

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {projId: 1, task: 'task 1', status: 'completed'}
1: {projId: 1, task: 'task 2', status: 'completed'}
2: {projId: 1, task: 'task 3', status: 'completed'}
3: {projId: 1, task: 'task 4', status: 'completed'}
4: {projId: 1, task: 'task 5', status: 'completed'}
5: {projId: 1, task: 'task 6', status: 'completed'}
6: {projId: 1, task: 'task 7', status: 'completed'}
7: {projId: 1, task: 'task 8', status: 'completed'}
8: {projId: 1, task: 'task 9', status: 'completed'}
9: {projId: 1, task: 'task 10', status: 'completed'}

Need to check the status, and if it is completed for the whole array then return competed else return in progress. The code I tried is given below, but that returns for each object in array.

{GroupedData[key].map(status => ((status.status === "completed")? "Completed" : "In Progress"))} 

And I am writing this inside the return of export function in React.js

3 Answers 3

1

.map() method is used to change values in a given array and returns that new modified array. In your case, your code changes each value in your array to completed / in progress depending on the status of the object.

In your case, I think that using .some() or .every() will be the best way to go about this. In the code below you are checking that every project has status completed and if that returns true it assigns 'completed' to result, if not it assigns 'in progress'

const result = data.every((project) => project.status === 'completed') ? 'completed' : 'in progress';
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

yourArrayObject.every((itemObject) => itemObject.status === 'completed') ? 'Completed' : 'In Progress';

Comments

0

All the map function does is to take an array and return a modified one. You could use the some function with it, like so:

let GroupedData = [
  [
    { projId: 1, task: "task 1", status: "completed" },
    { projId: 1, task: "task 2", status: "completed" },
    { projId: 1, task: "task 3", status: "completed" },
    { projId: 1, task: "task 4", status: "completed" },
    { projId: 1, task: "task 5", status: "completed" }
  ],
  [
    { projId: 2, task: "task 1", status: "completed" },
    { projId: 2, task: "task 2", status: "completed" },
    { projId: 2, task: "task 3", status: "completed" },
    { projId: 2, task: "task 4", status: "in progres" },
    { projId: 2, task: "task 5", status: "completed" }
  ]
];
let result = GroupedData.map((d) =>
  d.some((p) => p.status !== "completed") ? "In Progress" : "Completed"
);
console.log(result);

Comments

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.