Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new developer in react. My question is
Example
I need to copy authors array to authors1 changing field name .Can you please help me 

<pre lang="Javascript">const authors = [  
              {  
                'id': 1,   
                'name': 'Ranjeet',   
                'email': '@ranjeet'  
              },  
              {  
                'id': 2,   
                'name': 'Adil',   
                'email': '@adil'  
              },  
          ];  

const authors1
[  
              {  
                'authorId': 1,   
                'authorName': 'Ranjeet',   
                'authorEmail': '@ranjeet'  
              },  
              {  
                'authorId': 2,   
                'authorName': 'Adil',   
                'authorEmail': '@adil'  
              },  
          ]   


What I have tried:

I tried to map array but it is not success. I searched in web I didn't find exact answer
Posted
Updated 11-Jul-22 22:11pm

1 solution

You won't find an exact answer to your question. A lot of programming is about finding a close-enough answer and adapting it to your requirements.

Use the Array.map method to map the elements in your array:
Array.prototype.map() - JavaScript | MDN[^]
JavaScript
const authors1 = authors.map(author => ({
    authorId: author.id,
    // Fill in the rest of the mapping here...
}));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900