How to map/loop 2 array values to anchor tag with href and text?


jaish

I am trying to map values ​​from 2 different arrays, one containing links and the other containing tab names . I have an anchor tag consisting of href and text string . Is there a way I can map/loop over the values ​​in the 2 arrays to provide the values. Apps are built into React.

array

const links = ['#all', '#aew', '#roh', '#impact']
const tabnames = ['All', 'AEW', 'ROH', 'Impact']

const classnames = ['One', 'Two', 'Three', 'Four']

code

<a 
 href='#link' 
>
  tabname
</a>

What happens if I try to map 3 arrays on a and one for the class name ?

code

<a 
 href='#link'
 className='classname'
>
 tabname
</a>
sschwei1

You can use .map()and pass a function with 2 parameters, the map function can have up to 3 parameters .map((element, [index], [array]) => ...), the index and array are optional, the index can be used to access the elements of the second array

Here's a cut-out code that should work for you (adjusted to edited answer):

...

{
  links.map((elem, index) => (
    <a href={elem} className={classnames[index]}>{tabnames[index]}</a>
  ))
}

...

If possible, I recommend combining all arrays into an array of objects, since all elements with the same index are used together, it makes sense to combine them, which would look like this:

const links = [{
    href: '#all',
    tabName: 'All',
    className: 'One'
  },{
    href: '#aew',
    tabName: 'AEW',
    className: 'Two'
  },{
    href: '#roh',
    tabName: 'ROH',
    className: 'Three'
  },{
    href: '#impact',
    tabName: 'Impact',
    className: 'Four'
}];

...

{
  links.map((el) => (
    <a href={el.href} className={el.className}>{el.tabName}</a>
  ))
}

...

Related


How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code but it does not update the url of the an

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code but it does not update the url of the an

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code, but it doesn't update the anchor tag's

How to replace some text in href of anchor tag in jQuery

Ram Singh I have few url anchors like "http%3a/test.dev.test.com/posts/test test" and I want to replace it with " http://test.dev.test.com/posts/test Test" on the window loading function, I have tried the following code, but it doesn't update the anchor tag's

Multiple anchor tag href values in JavaScript

Manish Singh I am creating multiple anchor tags dynamically. I want to get the href value of the clicked anchor tag in javascript. I am using the following code to do this javascript document.getElementById("aaa").href <a id="aaa" onclick="follow(this);" href

Multiple anchor tag href values in JavaScript

Manish Singh I am creating multiple anchor tags dynamically. I want to get the href value of the clicked anchor tag in javascript. I am using the following code to do this javascript document.getElementById("aaa").href <a id="aaa" onclick="follow(this);" href

How to pass array of values through anchor tag helper?

james I am usingAsp.Net Core 2.1 Pages public async Task OnGetAsync([FromQuery(Name = "status")] string[] myValues) { } I'm trying to pass in an array of values from it anchor tag helper, but all I receive myValuesis one item. If I manually type the query st

How to pass array of values through anchor tag helper?

james I am usingAsp.Net Core 2.1 Pages public async Task OnGetAsync([FromQuery(Name = "status")] string[] myValues) { } I'm trying to pass in an array of values from it anchor tag helper, but all I receive myValuesis one item. If I manually type the query st

How to open alert box in anchor tag <a href>?

Anuja Lamaheva I want to open an alert box when a certain link is clicked. How to do it? username You can also use the following code: <a href="#" id="link">Link</a> Javascript: $(document).on("click","#link",function(){ alert("I am a pop up ! "); }); eithe

How to focus on anchor tag without href="#"

username How to focus when using keyboard tabs? <a class="ui-datepicker-prev ui-corner-all" data-handler="prev" data-event="click" title="Prev"><span class="ui-icon ui-icon-circle-triangle-w">Prev</span></a> I try to use CSS like this ui-datepicker-prev:focus

How to open alert box in anchor tag <a href>?

Anuja Lamaheva I want to open an alert box when a certain link is clicked. How to do it? username You can also use the following code: <a href="#" id="link">Link</a> Javascript: $(document).on("click","#link",function(){ alert("I am a pop up ! "); }); or: <

How to use querySelector with anchor tag with href?

User 3242861 I'm trying to use dropzone and I have an anchor tag to which I need to add an event click, but the way I'm doing this returns an error. URL: <a href="#new-intervention">Criar Intervenção</a> Inquire: var myDropzone = this; this.element.querySele

How to open alert box in anchor tag <a href>?

Anuja Lamaheva I want to open an alert box when a certain link is clicked. How to do it? username You can also use the following code: <a href="#" id="link">Link</a> Javascript: $(document).on("click","#link",function(){ alert("I am a pop up ! "); }); eithe

How to focus on anchor tag without href="#"

username How to focus when using keyboard tabs? <a class="ui-datepicker-prev ui-corner-all" data-handler="prev" data-event="click" title="Prev"><span class="ui-icon ui-icon-circle-triangle-w">Prev</span></a> I try to use CSS like this ui-datepicker-prev:focus

How to focus on anchor tag without href="#"

username How to focus when using keyboard tabs? <a class="ui-datepicker-prev ui-corner-all" data-handler="prev" data-event="click" title="Prev"><span class="ui-icon ui-icon-circle-triangle-w">Prev</span></a> I try to use CSS like this ui-datepicker-prev:focus

How to focus on anchor tag without href="#"

username How to focus when using keyboard tabs? <a class="ui-datepicker-prev ui-corner-all" data-handler="prev" data-event="click" title="Prev"><span class="ui-icon ui-icon-circle-triangle-w">Prev</span></a> I try to use CSS like this ui-datepicker-prev:focus

How to do both onClick and href in anchor tag (<a>)

username I have a link like this: <a href="www.google.com" onClick="someFunction()">Click me</a> After clicking the link, it's executing the javascript function but it doesn't take me to the href link. How can I do this? Esco I think the easiest way is to mak

How to escape characters in href of anchor tag

Kuldeep dangi I want to escape three characters, these are apostrophe(') Double quotes(") backslash() my href value istest.html?key="test' me'"&event=3 I want to fix it phpby callingaddslashes function <a href="test.html?key="test' me'"&event=3">test</a> Note

How to open alert box in anchor tag <a href>?

Anuja Lamaheva I want to open an alert box when a certain link is clicked. How to do it? username You can also use the following code: <a href="#" id="link">Link</a> Javascript: $(document).on("click","#link",function(){ alert("I am a pop up ! "); }); or: <

How to open alert box in anchor tag <a href>?

Anuja Lamaheva I want to open an alert box when a certain link is clicked. How to do it? username You can also use the following code: <a href="#" id="link">Link</a> Javascript: $(document).on("click","#link",function(){ alert("I am a pop up ! "); }); eithe

How to focus on anchor tag without href="#"

username How to focus when using keyboard tabs? <a class="ui-datepicker-prev ui-corner-all" data-handler="prev" data-event="click" title="Prev"><span class="ui-icon ui-icon-circle-triangle-w">Prev</span></a> I try to use CSS like this ui-datepicker-prev:focus

How to focus on anchor tag without href="#"

username How to focus when using keyboard tabs? <a class="ui-datepicker-prev ui-corner-all" data-handler="prev" data-event="click" title="Prev"><span class="ui-icon ui-icon-circle-triangle-w">Prev</span></a> I try to use CSS like this ui-datepicker-prev:focus

How to escape characters in href of anchor tag

Kuldeep dangi I want to escape three characters, these are apostrophe(') Double quotes(") backslash() my href value istest.html?key="test' me'"&event=3 I want to fix it phpby callingaddslashes function <a href="test.html?key="test' me'"&event=3">test</a> Note