Nisha Patel
Problem
I've an array of objects:
I want to update the key name based on the operation string value (UPDATE/DELETE). I do not want to go through for loop, rename it. I want to do it ES6/ES7 way or using destructuring, not sure though.
So, If the operation is DELETE, rename idmuFlag => idm; UPDATE, rename idmuFlag => idmNew.
I'm assigning the value to a new key and deleting the old key. Can we rename the keys directly?
Solution
You can create a map (ie: object) that associates operations (delete and update in your case) to the new expected keys. If the operation of the current object is not in this map then you can return the current object as there aren't any changes to be made. Otherwise, if the operation is in the map, you can get the associated key with the operation and then return your new object with that key, using destructing to remove the idmuFlag:
Suggested blog
>How to get the date and time and display it in a defineProps in Vuejs?
>Why logged user object is all strings in Vuejs and Laravel 10?
>What is meant by progressive framework?
>How can I replace this.$parent in composition API in Vuejs?
>How do I fix an invalid route component in the new VueJs Project?
>How to get all the rows selected in Vuejs?
>How to set up a dynamic grid based on flex or grid in Vuejs?
>Create a project using Vue.js: Beginners Guide
>Authentication with Vue 3 and Firebase
>Plugins and Presets for Vuejs project
>Create a Vue.js application with CLI Services
>Create a project using Vue.js: Beginners Guide
>Create Vue.js application API