0

I have a json like this:

 const mycontacts = [
       {
          "fleet_name":"RancorService",
          "owner":"swapneil",
          "host_count":10,
          "environment":"RancorService/JP",
          "vip_name":"rancor-02.pdx.amazon.com",
          "region":"pdx",
          "lb_set":"pdx-internal-111-set",
          "alternatename":"RANCOR-02-PDX-80",
          "protocol":"HTTP",
          "viptag":"retail_general",
          "port":80,
          "lb_status":"NOT_DEDICATED_VIP",
          "current_req_per_sec":0.9495412127,
          "current_req_res_per_gb_per_sec_payload":0.0309901519,
          "req_per_sec_threshold":80,
          "req_res_per_gb_sec_threshold":4,
          "partition_proposed":"NO"

    },
       {
          "fleet_name":"RancorService",
          "owner":"swapneil",
          "host_count":11,
          "environment":"RancorService/JP",
          "vip_name":"rancor-01.pdx.amazon.com",
          "region":"pdx",
          "lb_set":"pdx-internal-57-set",
          "alternatename":"RANCOR-01-PDX-80",
          "protocol":"HTTP",
          "viptag":"retail_general",
          "port":80,
          "lb_status":"NOT_DEDICATED_VIP",
          "current_req_per_sec":0.927976804,
          "current_req_res_per_gb_per_sec_payload":0.030198045,
          "req_per_sec_threshold":80,
          "req_res_per_gb_sec_threshold":4,
          "partition_proposed":"NO"

    }
]

I want to rename all the keys as the following but the values of the keys should not change.

const changedkeys = { 'Fleet name', 'Owner', 'Host Count', 'Environment', 'Vip Name', 'Region', 'LBset', 'Alternate Name', 'Protocol', 'Vip tag', 'Port', 'LB status', 'Current Requirement(/sec)', 'Current Request Response Payload(/gb/sec)', 'Request Threshold(/sec)', 'Request Response Threshold(/gb/sec)', 'Partition Proposed' }

Only the key name is to be replaced with the new keys but the values of the keys should not change. How to achieve that??

1
  • i have tried simple assignment of variables. Commented Apr 27, 2020 at 5:25

1 Answer 1

1

If the renamed keys are mapped in orders, try using Object.fromEntries and Object.values with Array.prototype.map to generate the desired array of objects:

const mycontacts = [
    {
        "fleet_name": "RancorService",
        "owner": "swapneil",
        "host_count": 10,
        "environment": "RancorService/JP",
        "vip_name": "rancor-02.pdx.amazon.com",
        "region": "pdx",
        "lb_set": "pdx-internal-111-set",
        "alternatename": "RANCOR-02-PDX-80",
        "protocol": "HTTP",
        "viptag": "retail_general",
        "port": 80,
        "lb_status": "NOT_DEDICATED_VIP",
        "current_req_per_sec": 0.9495412127,
        "current_req_res_per_gb_per_sec_payload": 0.0309901519,
        "req_per_sec_threshold": 80,
        "req_res_per_gb_sec_threshold": 4,
        "partition_proposed": "NO"

    },
    {
        "fleet_name": "RancorService",
        "owner": "swapneil",
        "host_count": 11,
        "environment": "RancorService/JP",
        "vip_name": "rancor-01.pdx.amazon.com",
        "region": "pdx",
        "lb_set": "pdx-internal-57-set",
        "alternatename": "RANCOR-01-PDX-80",
        "protocol": "HTTP",
        "viptag": "retail_general",
        "port": 80,
        "lb_status": "NOT_DEDICATED_VIP",
        "current_req_per_sec": 0.927976804,
        "current_req_res_per_gb_per_sec_payload": 0.030198045,
        "req_per_sec_threshold": 80,
        "req_res_per_gb_sec_threshold": 4,
        "partition_proposed": "NO"

    }
];

const changedkeys = ['Fleet name', 'Owner', 'Host Count', 'Environment', 'Vip Name', 'Region', 'LBset', 'Alternate Name', 'Protocol', 'Vip tag', 'Port', 'LB status', 'Current Requirement(/sec)', 'Current Request Response Payload(/gb/sec)', 'Request Threshold(/sec)', 'Request Response Threshold(/gb/sec)', 'Partition Proposed'];

const newArray = mycontacts.map(contact => Object.fromEntries(Object.values(contact).map((v, i) => [changedkeys[i], v])));

console.log(newArray);

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.