0

I am having array of objects like below,

let array = [
   {
      "config_id":"123",
      "disable":"0",
      "duration":1,
      "endpoint":"",
      "exit_status":null,
      "handle":null,
      "id":"63",
      "info_from_system":"{\"failed_resource_request\":{\"keys\":{\"subtype\":{\"=\":[\"storage_node\"]},\"model\":{\"in\":[\"H500S\"]},\"type\":{\"=\":[\"hci\"]},\"cacheCard\":{\"=\":[\"Radian\"]}},\"job_id\":\"63\",\"pool_id\":\"7\",\"quantity\":\"1\",\"id\":\"496\"}",
      "job_charge_id":"162",
      "job_id":"63",
      "priority":"8",
      "product_version":null,
      "workdir":null,
      "workload_id":null
   },
   {
      "config_id":"0987",
      "disable":"0",
      "duration":1,
      "endpoint":"",
      "exit_status":null,
      "handle":null,
      "id":"98",
      "info_from_system":"{\"failed_resource_request\":{\"keys\":{\"subtype\":{\"=\":[\"storage_node\"]},\"model\":{\"in\":[\"H500S\"]},\"type\":{\"=\":[\"hci\"]},\"cacheCard\":{\"=\":[\"Radian\"]}},\"job_id\":\"63\",\"pool_id\":\"7\",\"quantity\":\"1\",\"id\":\"1234\"}",
      "job_charge_id":"162",
      "job_id":"98",
      "priority":"8",
      "product_version":null,
      "workdir":null,
      "workload_id":null
   }
]

I need to remove double quotes to the key "id" (means, converting from string to Number) alone and not for other keys.

Tried the following method, but no luck.

array.replace(/\'/gid,"")

Is there anyway to achieve this. Any help will be much appreciated .

kalaiyarasi M
  • 261
  • 2
  • 11

1 Answers1

0

If your array is an array, then array.replace won't work.

Use .map instead, to take the id key and cast it to a number, while leaving the other keys alone with rest parameters:

array.map(({ id, ...rest }) => ({ id: Number(id), ...rest }));

const array = [
   {
      "config_id":"123",
      "disable":"0",
      "duration":1,
      "endpoint":"",
      "exit_status":null,
      "handle":null,
      "id":"63",
      "info_from_system":"{\"failed_resource_request\":{\"keys\":{\"subtype\":{\"=\":[\"storage_node\"]},\"model\":{\"in\":[\"H500S\"]},\"type\":{\"=\":[\"hci\"]},\"cacheCard\":{\"=\":[\"Radian\"]}},\"job_id\":\"63\",\"pool_id\":\"7\",\"quantity\":\"1\",\"id\":\"496\"}",
      "job_charge_id":"162",
      "job_id":"63",
      "priority":"8",
      "product_version":null,
      "workdir":null,
      "workload_id":null
   },
   {
      "config_id":"0987",
      "disable":"0",
      "duration":1,
      "endpoint":"",
      "exit_status":null,
      "handle":null,
      "id":"98",
      "info_from_system":"{\"failed_resource_request\":{\"keys\":{\"subtype\":{\"=\":[\"storage_node\"]},\"model\":{\"in\":[\"H500S\"]},\"type\":{\"=\":[\"hci\"]},\"cacheCard\":{\"=\":[\"Radian\"]}},\"job_id\":\"63\",\"pool_id\":\"7\",\"quantity\":\"1\",\"id\":\"1234\"}",
      "job_charge_id":"162",
      "job_id":"98",
      "priority":"8",
      "product_version":null,
      "workdir":null,
      "workload_id":null
   }
];

const output = array.map(({ id, ...rest }) => ({ id: Number(id), ...rest }));
console.log(output);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320