After looking at tutorial provided on this ReactJS tutorials page where the reference of .map is made to display Comment objects, one may get confused that “map” is ReactJS feature. As a matter of fact, this is an standard Javascript function which could be called on any array. Check out the MDN Documentation on this.
If you have worked on languages such as python (apply method), or R (lapply method), you could related “map” function as a method to which a function is passed with parameter representing reference of object stored in array. When “map” is called, the function is applied to each of the object stored in array. The “map” returns a new array consisting of objects which might be created using objects of passed array.
The general syntax is:
array.map(func)
where func should take one parameter.
As mentioned in above text, the return value of array.map is another array.
var newarr = [1,2,3,4].map( function(item) { return item * 5; } );
newarr is [5,10,15,20]
In example below, notice some of the following:
Following is how the below code sample would be displayed on HTML:
<div id="content"></div> <script type="text/jsx"> var profilesJson = [ {name: "Pete Hunt", country: "USA"}, {name: "Jordan Walke", country: "Australia"}]; var Profile = React.createClass({ render: function(){ return( <div> <div>Name: {this.props.name}</div> <div>Country: {this.props.country}</div> <hr/> </div> ); } }); var UserProfiles = React.createClass({ render: function(){ var allProfiles = this.props.profiles.map(function(profile){ return ( <Profile name={profile.name} country={profile.country} /> ); }); return( <div>{allProfiles}</div> ); } }); React.render( <UserProfiles profiles={profilesJson}/>, document.getElementById( "content"));</script>
Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…
Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…
Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…
Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…
The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…
Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…