简体   繁体   English

渲染嵌套数据(来自另一个数组的 object 中的数组的对象)来自后端 - ReactJS (JSX)

[英]Rendering nested data (objects from an array inside an object of another array ) From backend - ReactJS (JSX)

I have JSON data i am trying to render to the front end of my application.我有 JSON 数据,我正在尝试呈现到我的应用程序的前端。 and I am trying to get the name of all the objects inside the betTypes array to the front end as a List.我试图将 betTypes 数组中所有对象的名称作为列表获取到前端。 So far, I am only able to query a single object from the nested array.到目前为止,我只能从嵌套数组中查询单个 object。

const data = {
  games: [
    {
      name: 'BlackJack',
      slug: 'American Blackjack',
      category: 'Card games',
      image: '/images/bj.jpg',
      betTypes: [
        {
          name: 'Flush',
          det: 'Flush',
          category: 'American Blackjack',
          description:
            'The player’s two initial cards and dealer’s first cards are all of the same suits and have different values (it is also valid if two of the three cards are of the same value)',
          image: '',
        },
        {
          name: 'Mixed Color Pair',
          det: 'MXP',
          category: 'American Blackjack',
          description:
            'When the player gets two initial cards as a pair (same value) of different colour and different suit.',
          image: '',
        },
      ],
    }, 
},

This is what my front-end code looks like这就是我的前端代码的样子

function GameScreen() {
  const params = useParams();
  const { slug } = params;

  const [{ loading, error, game }, dispatch] = useReducer(reducer, {
    game: [],
    loading: true,
    error: '',
  });
  // const [games, setGames] = useState([]);
  useEffect(() => {
    const fetchData = async () => {
      dispatch({ type: 'FETCH_REQUEST' });
      try {
        const result = await axios.get(`/api/games/slug/${slug}`);
        dispatch({ type: 'FETCH_SUCCESS', payload: result.data });
      } catch (err) {
        dispatch({ type: 'FETCH_FAIL', payload: err.message });
      }

      //setGames(result.data);
    };
    fetchData();
  }, [slug]);

  return loading ? (
    <div>
      <LoadingSpin />
    </div>
  ) : error ? (
    <div>{error}</div>
  ) : (
    <div>
      <Row>
        <Col md={12}>
          <h1>{game.name}</h1> <hr />
          <img className="img-large" src={game.image} alt={game.name} />
        </Col>
        <Col md={3}>
          <ListGroup>
            <ListGroup.Item style={{ marginTop: '10px' }}>
              <h6>{game.betTypes[3].name}</h6>
            </ListGroup.Item>
          </ListGroup>
        </Col>
        <Col md={3}></Col>
      </Row>
    </div>
  );
}

I need help sorting it out please.请帮我解决一下。

    <ListGroup>
         {game.betTypes.map((item)=>
            <ListGroup.Item key={item.name} style={{ marginTop: '10px' }}>
                <h6>{item.name}</h6>
            </ListGroup.Item>)
         }
    </ListGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从对象的多层嵌套数组reactjs创建嵌套的JSX列表项 - Create nested JSX list items from multi-level nested array of objects reactjs 从多级嵌套对象数组 reactjs 创建嵌套的 JSX 元素 - Create nested JSX elements from multi-level nested array of objects reactjs 将JSX元素作为数组内另一个JSX元素的子元素推送(ReactJS) - Push JSX element as a child of another JSX element inside an Array (ReactJS) 检查一个数组中的 object 是否包含在另一个对象数组中 - Check if object from one array is included inside another array of objects ReactJs:如何通过单击从对象数组中显示 object 的特定数据 - ReactJs : How to display a specific data of an object from an array of objects with a click 从对象到带有嵌套对象的数组 - from object to array with nested objects 如何将数据从嵌套数组(在对象数组中)推送到新数组? 同时保持其嵌套结构 - How to push data from a nested array, inside an array of objects, to a new array? while maintaining its nested structure 从 axios 响应 object 渲染对象数组 - rendering an array of objects from the axios response object 从 Javascript 中嵌套数组内的对象中过滤数据的最佳方法 - Best way to filter data from objects inside nested array in Javascript 从对象内部的对象数组到数组内部的对象数组处理数据 - Manipulating data from array of objects inside object to array of objects inside array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM