Utilizing the power of array mapping in JavaScript to dynamically display information

After retrieving an array of items from an Object, I noticed that the array contains 5 items. Here is a snapshot of how it looks:

https://i.sstatic.net/RgTtH.jpg

For storage, I have placed the array in my state as:

this.state={
    serviceDetails: planDetails[0].nbs_plans.map(service => service.extension_attributes.productServicetList.map(name=>name.name)),
}

Now, my goal is to render the array in my component using the map function:

 {
             this.state.serviceDetails.map((serviceName) => {
               return (
<SelectableChips initialChips={serviceName} />
               );
             })
           }

However, the issue I'm facing is that all the arrays are rendering in the same location, as shown below:

https://i.sstatic.net/sNGM9.jpg

What I expect is for each "See more" to appear in its respective section - first "See more" with the first section, second "See more" with the second section, and so on.

The complete code for this component is provided below:

//This is an example code for FlatList//
import React from 'react';
import {
 StyleSheet, FlatList, Text, View, Alert , Image, TouchableOpacity 
} from 'react-native';
import SelectableChips from '../../../components/Chip/SelectableChips';
import CheckBox from '../../../components/CheckBox';
import propTypes from 'prop-types';
import Service from './Service';
import { planDetails } from '../../../api/mockedData/PlanServiceDetails';
class Selected_Items_Array {

  constructor() {
    ...
  }

  ...
}
export default class Services extends React.Component {
  ...
}

If anyone can provide guidance on how to proceed, I would greatly appreciate it.

Answer №1

For every plan that contains a productServicetList, a serviceDetails object is created with the planName as the key and productServicetList as the value.

this.state={
    serviceDetails: planDetails[0].nbs_plans.reduce(
      (acc, service) => ({
        ...acc,
        [service.name]: service.extension_attributes.productServicetList.map(
          name => name.name
        )
      }),
      {}
    );
}

Next, the serviceDetails are rendered in the following manner

  {
    this.state.serviceDetails[item].map(serviceName => {
      return <SelectableChips initialChips={serviceName} />;
    });
  }

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

Is there a regular expression that can identify whether a string is included in a numbered list?

Struggling with creating a regular expression to determine if a string is part of a numbered list like those in word processors. Need it to return true only if the string starts with a number, followed by a full stop and a space. Easy for single or doubl ...

Insert a THREE.Points element into the scene: Error in THREE.Object3D.add: The object being added is not a valid instance of THREE.Object3D (

Trying to incorporate a system of particles, a THREE.Points element into the scene has resulted in the following error: "THREE.Object3D.add: object not an instance of THREE.Object3D. undefined" The code used for this is as follows: var backCount = 1800; ...

Problem with Selenium WebDriver: Some HTML elements are not being displayed

I am currently working on a python web scraper. Here is the code I have written: from selenium.webdriver.chrome.options import Options from selenium import webdriver from bs4 import BeautifulSoup chrome_options = Options() chrome_options.add_argument(&qu ...

Capturing click events on a stacked area chart with nvd3

Could someone assist me in capturing the click event on a nvd3 stacked area chart? I have successfully managed to capture tooltip show and hide events but am struggling with the click event. How can I obtain information about the clicked point? Please help ...

When attempting to access AJAX JSON properties using an index within a promise, the result returned is undefined

I have a quiz app that communicates with my server's API using MongoDB. I am trying to access the response indexes in this way: setTimeout(() => { axios.get('/api/ninjas') .then(function (questions) { var data = questions.d ...

What is the process for extracting a value by clicking a button in react native?

I am currently working on a simple form in my React Native project that consists of two text inputs and a button. The project was created using the React Native CLI. const Signup = () => { const [username, setUsername] = useState('') const ...

Error Alert: Attempting to access the 'prototype' property of an undefined value has resulted in a TypeError after switching to react-scripts version 4.0.3

I encountered a problem where I received the following error message: (Uncaught Error: Module build failed (from ./node_modules/source-map-loader/dist/cjs.js)). After attempting to resolve it by using npm install, a new error popped up in the console: path ...

Configuring a Selenium Firefox profile

While performing our tests, we came across an issue with how FireFox handles events when the browser is not in focus. We discovered that this problem can be resolved by setting up a FireFox profile with the preference "focusmanager.testmode" set to true ( ...

Incorporate External Web Page Information by Clicking a Button

Apologies if this question has already been asked, but despite my efforts to explore the available resources, I have not found a solution to my issue. I currently have an input field where users can enter their website, keyword, or industry. When they cli ...

Is it possible to permanently alter HTML values with JavaScript?

Can a html value be permanently modified using javascript? I am trying to edit a local file. Below are the codes I'm using: function switchPic(){ top.topPage.activeDef = top.topPage.document.getElementById('h1'); top.topPage.activeDef. ...

How can we control the timing of elements displaying on a web page in Javascript?

I've been attempting to implement a scrolling feature on my webpage using JavaScript, but whenever I run it, the content appears all at once instead of scrolling. The $("#Menu").html('') function doesn't seem to clear the screen properl ...

Javascript/Typescript Performance Evaluation

I am looking to create a visual report in the form of a table that displays the count of each rating based on the date. The ratings are based on a scale of 1 to 5. Below is the object containing the data: [ { "Date": "01/11/2022", ...

Encountering the React Native error message "TypeError: Object(...) is not a function" while using react navigation stack

I am currently facing an issue with my react-navigation-stack. My suspicion lies in the dependencies, but I am uncertain whether that is the root cause. The objective at hand is to create a text redirecting to another page. If there is any irrelevant code, ...

Retrieving information from a text document by means of data-src with the assistance of jQuery

Trying to extract text from a .txt file using jQuery while utilizing Bootstrap. New to web design with some coding experience. Button in HTML: <button type="button" class="btn btn-primary-outline-btn text-btn" data-toggle="modal ...

Develop a personalized event using JavaScript and activate it

I have encountered a problem with a Google ad that expands on click and closes when the close button is hit. However, due to changing conditions, I now need it to expand first and then automatically close after a certain time. Additionally, every time it e ...

jquery plugin for creating Excel-like filters

I am in the process of creating Excel-like filtering for my dynamic table. To achieve this, I am utilizing a jQuery plugin that can be found at https://github.com/chestercharles/excel-bootstrap-table-filter. The reason why I chose this plugin is because it ...

Dynamically generating fields in JavaScript causes the fields to mysteriously vanish

Below is the JavaScript code I am working with: <script language="javascript"> function addInput() { document.getElementById('text').innerHTML += "<input type='text' value='' name='a1[]' size='60&a ...

Leveraging JavaScript to extract data from a JSON file upon clicking a button

Currently, I am working on a problem where the user enters values into a search box, clicks the search button, and then with the onClick event, the search terms are compared to values in a JSON file. I do not have knowledge of jQuery, so if there is a solu ...

The concept of AJAX send function and toggling button visibility

I am facing a challenge with a textarea and send button on a panel that is displayed after entering the first character. The visibility of the button is controlled by the isButtonVisible() method. Once clicked, it sends the text from the textarea using the ...

What is the most efficient way to retrieve a substantial volume of data from Mongodb quickly, especially when the query results require processing before transmitting to the client?

My project involves a large Mongodb database paired with a React front-end and Node back-end, housing around 100000 documents. Indexing has been completed and Redis is already in use. The search feature implemented allows users to find relevant documents b ...