You mean you have an array like the following and you are trying to access the elements in the arrays that are within the objects?
/* This code is not 100% identical to your code,
but the idea is the same. I think. */
var ideas = [
{"title": "First idea", "premises": ["Premise one", "Premise two"], "conclusion": "Example conclusion"},
{"title": "Second idea", "premises": ["Premise one", "Premise two"], "conclusion": "Example conclusion"},
{"title": "Third idea", "premises": ["Premise one", "Premise two"], "conclusion": "Example conclusion"}
]
To map over it, you can do the following:
/* This code is not identical to your code,
but I think that the idea is the same.
You just need to adjust it to your own situation. */
/* First of all, ideas array should be
mapped like in the following */
ideas.map(val => {
/*
Now each object will be in val, to access premises arrays,
dot notation will help in doing that: val.premises = three arrays
The arrays should be mapped in order to get each element
contained therein */
val.premises.map(x => {
/* x will be equivalent to each element in the array */
console.log(x); /*output:
Premise one
Premise two
Premise one
Premise two
Premise one
Premise two
*/
});
});
This is my code. It is the same as yours, except instead of val I have idea, instead of x I have premise, and instead of a console log I have a paragraph. It’s quite puzzling to me that this won’t work. Here’s the error:
Perhaps it’s meant to have a return statement - thanks, I’ll fix that! I don’t believe it’s required, though, because when I remove the second map function, the first actually works fine, without a return statement, so I doubt this is the core issue either.
It seems like this error is legitimately a little puzzling? Thanks very much for helping out guys.
Based on the structure you show, would this work? My code below assumes your code was nested in a div which is rendered. It may not be and you can unjust the code accordingly.
I think you solved the problem by implementing keys in the map function. What made you think that needed to be done (which it clearly did - thanks by the way)?
It all makes sense now. Maybe I should have learned React from FreeCodeCamp’s curriculum - I had used other resources, and taking a look at it now, I wish I’d done this earlier. Thanks very much for the help!
Hi can you please help i am having error:
TypeError: undefined is not an object (evaluating “premises.map”)
BTW i am using the code in react native
Thanks in advance