JavaScript Given an array of objects, how do I replace the key name with another whilst keeping the value from the original key?

I'm trying to take an array of objects and, without mutating the original array, rename a specific key in each object.

I've created a new variable and assigned it to the result of using 'map' on my original array (to avoid mutation). Within the 'map' method, I've tried to deconstruct the 'keyToChange' parameter and spread the rest of the values to the 'newKey'.

const renameKeys = (array, keyToChange, newKey) => {
  const spreadArray = array.map(({keyToChange: newKey, ...rest}) => ({newKey, ...rest}));
    return spreadArray;
   };

The above code returns 'newKey: [undefined]' followed by the original array of objects.

Any help would be greatly appreciated.

Answer:1

You need a computed property name instead of a (not given) propery keyToChange.

const spreadArray = array.map(({ [keyToChange]: newKey, ...rest }) => ({ newKey, ...rest }));
//                               ^^^^^^^^^^^^^
Answer:2

Use square brackets notation if you want to use dynamic value as key. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors */

 const spreadArray = array.map(({ [keyToChange]: newKey, ...rest}) => ({newKey, ...rest}));
Answer:3

I tried finding the solution to my problem, but couldnt find it, and was looking for some "best practice examples". I have a nodejs express application and my functions are split in files. For example ...

I tried finding the solution to my problem, but couldnt find it, and was looking for some "best practice examples". I have a nodejs express application and my functions are split in files. For example ...

  1. javascript node js await
  2. js node js await
  3. javascript async await nodejs
  4. javascript await node.js
  5. async await javascript node js

I often use this strategy to my java code in order to make a Collection read only for the outside world, but avoid big/often clonings: public abstract class MyClass { List<Long> myIds; ...

I often use this strategy to my java code in order to make a Collection read only for the outside world, but avoid big/often clonings: public abstract class MyClass { List<Long> myIds; ...

  1. equivalent javascript
  2. equivalent javascript pdf
  3. equivalent javascript book
  4. equivalent javadoc c#
  5. java equivalent of python dictionary
  6. java equivalent of linq
  7. javadoc equivalent for python
  8. javascript equivalent of document ready
  9. javascript equivalent of jquery find
  10. java equivalent of pass
  11. javascript equivalent of python range
  12. java equivalent of var
  13. java equivalent of struct
  14. javascript equivalent of linq select
  15. java equivalent of using
  16. javascript equivalent of pass
  17. java equivalent of dictionary
  18. java equivalent of pandas
  19. javascript equivalent of python dictionary
  20. java equivalent of c# dictionary

I'm trying to pass a string parameter using the link. but it seems that computed or methods property cannot return the parameter value. The whole component stops rendering when I use computed property....

I'm trying to pass a string parameter using the link. but it seems that computed or methods property cannot return the parameter value. The whole component stops rendering when I use computed property....

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. cancun
  6. canada goose
  7. cane corso
  8. canes
  9. canadian prime minister
  10. cancun flights
  11. canker sore
  12. candy
  13. cancer
  14. canon
  15. canvas prints
  16. cancer sign
  17. candace owens
  18. canada news
  19. candytopia
  20. canlis

I've got an infinite loop inside my while loop and I can't find the cause. It's a simple function that returns the sum of the argument's digits. I use a while loop because it needs to add up the ...

I've got an infinite loop inside my while loop and I can't find the cause. It's a simple function that returns the sum of the argument's digits. I use a while loop because it needs to add up the ...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. canada goose
  6. cancun flights
  7. cane corso
  8. canes
  9. canadian prime minister
  10. canker sore
  11. candy
  12. cancer
  13. cancun
  14. canon
  15. canvas prints
  16. cancer sign
  17. candace owens
  18. canada news
  19. candytopia
  20. canlis