- Forums
- ionic
- OBJECT map() in React - How To Use Ionic Map In OBJECT With React Loop map 1 - 10
This Page Contains information about OBJECT map() in React - How To Use Ionic Map In OBJECT With React Loop map 1 - 10 By me in category ionic with 0 Replies. [5014], Last Updated: Mon Jun 24, 2024
me
Sun Sep 11, 2022
0 Comments
570 Visits
Using React JSX, These are three ways you can loop through an Object in React. Hop[e this helps:
Object Example:
const otherTasks = {
'Homework': { url: '' },
'Sightwords': { url: 'https://sightwords.websor.com/' },
'Math': { url: 'https://www.websor.com/pre-school/math/rowley/' },
}
Render in JSX:
{Object.entries(otherTasks).map(([taskName,otherTask], index) =>
<div key={index}>... {taskName} = {otherTask.url}</div>
Loop Through X Amount of Times: (ei: 10 Times)
{[...Array(10)].map<any>((e, i) => { // FOR TYPESCRIPT
{[...Array(10)].map((e, i) => {
return <span className="any" key={i}>{i}</span>
})}
Example 1 - Ionic Typescript
// make your imports here, useState not required just using as example
import { useState } from "react";
// declare properties types
interface exampleObject {
url: string;
iosIcon: string;
mdIcon: string;
title: string;
}
// notice plural and singular here exampleObjects and exampleObject
// THE OBJECT
const exampleObjects: exampleObject[] = [
{
title: "Spell",
url: "/page/Spell",
iosIcon: mailOutline,
mdIcon: mailSharp,
},
];
const Menu: React.FC = () => {
return (
// loop through the object
{exampleObjects.map((exampleObject, index) => {
return (
<IonItem lines="none" key={index}>
<IonLabel>{exampleObject.title}</IonLabel>
<IonRadio slot="start" value={exampleObject.title} />
</IonItem>
);
})}
);
};
export default Menu;
// make your imports here, useState not required just using as example
import { useState } from "react";
// declare properties types
interface exampleObject {
url: string;
iosIcon: string;
mdIcon: string;
title: string;
}
// notice plural and singular here exampleObjects and exampleObject
// THE OBJECT
const exampleObjects: exampleObject[] = [
{
title: "Spell",
url: "/page/Spell",
iosIcon: mailOutline,
mdIcon: mailSharp,
},
];
const Menu: React.FC = () => {
return (
// loop through the object
{exampleObjects.map((exampleObject, index) => {
return (
<IonItem lines="none" key={index}>
<IonLabel>{exampleObject.title}</IonLabel>
<IonRadio slot="start" value={exampleObject.title} />
</IonItem>
);
})}
);
};
export default Menu;
EXAMPLE2
This is another way you can loop through a JSON object:
{users && users.length > 0 && users.map((user, index) => (
<li key={user.id}>{user.name}</li>
))}
Example 3:
SAME as EXAMPLE2:
{users.map((user, index) => {
return (
<li key={user.id}>{user.name}</li>
);
})}