-2

I have an array in the below format

{
  "Tasks": [
    {
      "TaskID": 303691,
      "TaskName": "Test1",
      "TaskType": "Internal",
      "Status": "Processing",
      "IsApproved": false,
      "RowNumber": 1
    },
    {
      "TaskID": 303694,
      "TaskName": "Test2",
      "TaskType": "External",
      "Status": "Processing",
      "IsApproved": true,
      "RowNumber": 2
    },
    {
      "TaskID": 303708,
      "TaskName": "Test3",
      "TaskType": "Internal",
      "Status": "Error",
      "IsApproved": false,
      "RowNumber": 3
    }
  ],
  "TotalRows": 3
}

I have to format the array(TaskDetail["TotalRows"]) before displaying it by converting "IsApproved": to 'No' or 'Yes' : based on false or true.

I am getting the array details like below

getTaskList(parentId: number, userName: string) {       
        this.url = `api/${userName}/tasks/${parentId}`;
        this.taskSubscription =
            this.http.post(this.url)
            .pipe(map(o => o as TaskDetail[]))
            .subscribe((taskDetail: TaskDetail[]) => {
                this.userTasks$.next(TaskDetail["TotalRows"];
                );
                },
                    error => {
                        this.userTasks$.next(null);
                        this.errorHandlerService.handleError(error);
                    });
    }
So the final array will be like follows

{
  "Tasks": [
    {
      "TaskID": 303691,
      "TaskName": "Test1",
      "TaskType": "Internal",
      "Status": "Processing",
      "IsApproved": No,
      "RowNumber": 1
    },
    {
      "TaskID": 303694,
      "TaskName": "Test2",
      "TaskType": "External",
      "Status": "Processing",
      "IsApproved": Yes,
      "RowNumber": 2
    },
    {
      "TaskID": 303708,
      "TaskName": "Test3",
      "TaskType": "Internal",
      "Status": "Error",
      "IsApproved": No,
      "RowNumber": 3
    }
  ],
  "TotalRows": 3
}

Can any one help me how to do that?

3

1 Answer 1

1

In cases like this, I like to create a new field and give it "_F" - for example, IsApproved_F. So I still have original value if needed. You don't need to do this if you don't like it. Then as suggested here Using map, how do I transform a yes and no string in an objects property into a boolean true and false value in javascript and as already pointed out in the comments use a map. You can also use the for loop.

let i = yourObject.Tasks.map(function(task){
     task.IsApproved? task.IsApproved = "Yes": task.IsApproved= "No";
     return task;
});
Sign up to request clarification or add additional context in comments.

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.