React's nested map function is failing to display any results

My objective is to loop through nested arrays in the response data, which is an array containing other arrays. However, I am encountering an issue where the data is not being displayed for some reason.

Here is an example of one item in the response:

id: 548058
image: "https://testing.com/recipeImages/548058-312x231.jpg"
imageType: "jpg"
likes: 1655
missedIngredientCount: 2
missedIngredients: Array(2)
0: {id: 1089003, amount: 1, unit: 'small', unitLong: 'small', unitShort: 'small', …}
1: {id: 98940, amount: 4, unit: '', unitLong: '', unitShort: '', …}
length: 2
[[Prototype]]: Array(0)
title: "Brie and Apple Panini – 6 Points"
unusedIngredients: []
usedIngredientCount: 1
usedIngredients: [{…}]

Below is the code I have written:

  {recipes.map((item) => {
    return (
      <View key={item.id} style={styles.recipe}>
        <Image source={{ uri: item.image }} style={styles.img} />
        <Text style={styles.bold}>{item.title}</Text>
        <Text>Missing ingredients: {item.missedIngredientCount}</Text>
        {item.missedIngredientCount > 0 && (
          <View>
            {item.missedIngredients.map((ing) => {
              <View>
                <Text>
                  {ing.name} ({ing.original})
                </Text>
                <Text>
                  Amount: {ing.amount}
                  {ing.unitShort}
                </Text>
              </View>;
            })}
          </View>
         )}
  })}

Even though missedIngredientCount is greater than 0, nothing is displaying on the screen. I have tried removing the condition without success.

The part that is not showing is item.missedIngredients.map.

Answer №1

The reason for the issue is that you forgot to add a return statement inside the inner map function.

 {recipes.map((item) => {
return (
  <View key={item.id} style={styles.recipe}>
    <Image source={{ uri: item.image }} style={styles.img} />
    <Text style={styles.bold}>{item.title}</Text>
    <Text>Missing ingredients: {item.missedIngredientCount}</Text>
    {item.missedIngredientCount > 0 && (
      <View>
        {item.missedIngredients.map((ing) => {
         return (<View>
            <Text>
              {ing.name} ({ing.original})
            </Text>
            <Text>
              Amount: {ing.amount}
              {ing.unitShort}
            </Text>
          </View>;)
        })}
      </View>
     )}
  })}

Answer №2

{recipes.map((item) => {
    return (
      <View key={item.id} style={styles.recipe}>
        <Image source={{ uri: item.image }} style={styles.img} />
        <Text style={styles.bold}>{item.title}</Text>
        <Text>Missing ingredients: {item.missedIngredientCount}</Text>
        {item.missedIngredientCount > 0 && (
          <View>
            {item.missedIngredients.map((ing) => {
              return(
              <View>
                <Text>
                  {ing.name} ({ing.original})
                </Text>
                <Text>
                  Amount: {ing.amount}
                  {ing.unitShort}
                </Text>
              </View>;
               )
            })}
          </View>
         )}
  })}

You should ensure to return from the inner map as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

text:js Using a StringBuilder

As a newcomer to the .NET platform, I am eager to learn how to pass a JS function with curly brackets as a string inside StringBuilder.AppendFormat() in C#. Below is my desired approach: StringBuilder sb = new StringBuilder(); sb.AppendFormat(@"<s ...

What is the best way to obtain a unique result for a single cell without encountering any duplicates when using the Angular

Currently, I have functional code that successfully passes data from a dialog window to a table. Everything works fine for a single row, but when I try to add multiple rows to the table, I get results for several columns simultaneously. Is there a way to o ...

Enhancing security with Mongoose's bcrypt for asynchronous password setting and saving

I've defined a mongoose schema that includes a method to set a password using bcrypt: UserSchema.methods.setPassword = function (password) { bcrypt.hash(password, saltRounds).then(function (hash) { this.hash = hash; }); }; When creating a us ...

Stacking sheets of hole-punched paper on top of one another, create a visually captivating

I am in search of a way to create those distinctive black dots with papers displayed here: body { background: linear-gradient(#ccc, #fff); font: 14px sans-serif; padding: 20px; } .letter { background: #fff; box-shadow: 0 0 10px rgba ...

What is the best way to bring my file into app.js using JavaScript?

I've been attempting to include a JavaScript file into app.js within the create-react-app package. I tried the following code to import my file: Just a heads up: my file is located in a folder called components within the Navigation folder. import s ...

Does the image alter based on the selection in the dropdown menu?

When I utilize the Templating Example from the select2 library, everything seems to work for the first dropdown value change. The correct image is previewed as expected. However, when trying to change the value a second time, a second image is appended but ...

displaying HTML on the web page only, without appearing in the text input field

update1: I have updated the image to provide better clarity https://i.sstatic.net/hYLsI.png I am currently working on implementing chip filters similar to Google Flights. When selecting "sports," it currently displays "sports1" and replaces "sports" with ...

Obtaining legitimate CSS property names for React dynamically

I am looking to create a dropdown menu in React that allows users to select camelized CSS properties (such as width, color, textAlign) for inline styles. Instead of manually inputting all the options for the dropdown, I had the idea of using the React.CSS ...

Using jQuery, transform JSON into an array

After running my PHP code, I have the following result: $result = '[{"MFG_NAME":"ABC","CONCATED_MKT_SHARE":"01-MAR-14|0.59"},{"MFG_NAME":"XYZ","CONCATED_MKT_SHARE":"01-MAR-14|0.87"},{"MFG_NAME":"ABC","CONCATED_MKT_SHARE":"01-APR-14|0.25"},{"MFG_ ...

Collaboratively accessing a shared constant in two separate JavaScript files

I am diving into the world of JavaScript and Node.js. I am currently experimenting with Puppeteer to extract the text value of a tag and store it in a constant variable. However, I am encountering difficulties when trying to integrate this value into my ...

Only refresh the content when there are updates from the ajax call

Currently, I am populating an HTML table with data retrieved through an AJAX request. The AJAX call is made at regular intervals of X seconds. I am specifically looking for a way to update the table only when the new data fetched from the AJAX call diffe ...

Storing and Editing Collection of Elements

My latest project involves creating a basic web scraping tool that gathers information on apartment prices from a specific webpage. Each time the tool runs, it compiles an array of objects with details such as date, time, floor plan name, bed number, floor ...

Mastering the art of chaining HTTP requests with RxJS for optimal results

I have a task that involves making multiple HTTP calls sequentially, examining the result of each call before proceeding to the next one. Depending on the response, I may need to display a message, continue to the next call, or break the chain. Additionall ...

Caution: "Detected x instances with duplicate IDs" in the REACTJS API REST

I am facing an issue with my form. The console is displaying a warning "Found x(number of books) elements with non-unique id". As a result, the information displayed for each book is duplicated because the id is not being recognized. I need help in ident ...

Vue.js: Issue with updating list in parent component when using child component

I'm encountering an issue when utilizing a child component, the list fails to update based on a prop that is passed into it. When there are changes in the comments array data, the list does not reflect those updates if it uses the child component < ...

Learn the process of dynamically adding components with data to a list of objects using React JS

In my current project, I am working with a component list that consists of MUI chips. These chips have specific props such as 'label' and 'callback', which I need to incorporate into the list when an onClick event occurs. Each chip shou ...

Can you please explain the purpose of this code block and whether it is necessary for me to use in my project

I possess highly sensitive information that needs to be handled with care. Can you explain the purpose of this code snippet and its significance? app.use(function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.hea ...

Enhance your slideshow with a stylish fade effect

I found this slideshow on codepen and decided to customize it to make it my own. While everything is working well, I wanted to incorporate a fade effect into the transition between slides. I'm unsure whether I should add something to the CSS file or m ...

calculating the rotation angle from the origin to a vector within a three-dimensional space using three.js

I am working with a vector in three-dimensional space. Is there a method to determine the angle of rotation from the origin to this vector on each axis? For example, if the vector is located at x=6, y=-10, z=20, what angle does the vector make with resp ...

The NativeAppEventEmitter does not return any value

I've been grappling with obtaining a logged in user access token for quite some time. I initially faced challenges with it in JavaScript, so I switched to Objective-C and managed to succeed. Following that, I referred to this RN guide: https://facebo ...