Home > other >  Array formatting issue
Array formatting issue

Time:01-25

I have an array in the below format.

[
    {
      "TaskID": 303,
      "TaskName": "Test1",
      "TaskType": "Internal",
      "Status": "Processing",
      "IsApproved": false,
      "RowNumber": 1
    },
    {
      "TaskID": 304,
      "TaskName": "Test2",
      "TaskType": "External",
      "Status": "Processing",
      "IsApproved": true,
      "RowNumber": 2
    },
    {
      "TaskID": 305,
      "TaskName": "Test3",
      "TaskType": "Internal",
      "Status": "Error",
      "IsApproved": false,
      "RowNumber": 3
    }
  ]

I wanted to get the the TaskID in the below format

"[303]","[304]","[305]"

Is it possible? if so please guide me.

I tried the below code, i am not getting the above format

let selectedIds = this.arrayList.map(item => {
            return {
                TaskID: item.TaskID
            };
        })

CodePudding user response:

Stackblitz output

If you want this output: ["[303]", "[304]", "[305]"]

let selectedIds1 = this.arrayList.map( item => "["  item.TaskID  "]" );
console.log(selectedIds1);
// ["[303]", "[304]", "[305]"]

However, if you actually want this output, as I guess: [303,304,305]:

let selectedIds2 = this.arrayList.map(item => item.TaskID);

console.log(selectedIds2);
// [303,304,305]

If you want this output: [["303"],["304"],["305"]]

let selectedIds3 = this.arrayList.map( item => [item.TaskID?.toString()] );
console.log(selectedIds3);
// [["303"],["304"],["305"]]


NOTE: Remove .toString() if you want the id as number:
// [[303],[304],[305]]

  •  Tags:  
  • Related