How can I map though this array? #418
-
Hello, Can someone help me map this if it's even possible?
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 15 replies
-
We have some docs on this here: https://next-intl-docs.vercel.app/docs/usage/messages#arrays-of-messages Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
I've seen the docs but it's actually not the same. What I'm trying to map is a nested object. |
Beta Was this translation helpful? Give feedback.
-
Thank you. This works but how do I get rid of the typescript error? |
Beta Was this translation helpful? Give feedback.
-
For my part, I managed to get around the problem. It's not a very clean way, but it's got the merit of working. My idea was to reproduce a "kind of array". I give each sub-object in a main object, and I myself give the length of this array ("length" attribute). "items": {
"length": "5",
"0": {
"title": "title 1"
},
"1": {
"title": "title 2"
},
"2": {
"title": "title 3"
},
"3": {
"title": "title 4"
},
"4": {
"title": "title 5"
},
"5": {
"title":"title 6"
}
} After this, I can retrieve my "array" in my code as follows Array.from(Array(Number(t(`items.length`))).keys()).map((item: number) => {
// @ts-ignore
return <YourElement key={item} label={t(`items.${item}.title`)} />
}) Not so clean, but working when you know what you are doing 😀 |
Beta Was this translation helpful? Give feedback.
-
I understand that it's a little late, but there is an alternative solution for those who, like me, came across this topic and are looking for an answer to solve a problem where there is an array of objects. You just need to change the array a little and use
|
Beta Was this translation helpful? Give feedback.
The trick is to move the array to your React component:
Hope this helps!