简体   繁体   English

如何在我的产品 ID 页面中解构嵌套数组

[英]How to destructure nested array inside my product Id page

I'm trying to get the nested array inside the ProductId page.我正在尝试在ProductId页面中获取嵌套数组。 I'm getting the following error code:我收到以下错误代码:

cannot find undefined找不到未定义

I know that i'm doing wrong by writing Data.Items.find, but if not like this then How..?我知道我通过编写 Data.Items.find 做错了,但如果不是这样,那么如何..? I want to know how to get the Data.Items array to get it matched with it's id using match.params.我想知道如何使用 match.params 获取 Data.Items 数组以使其与其 ID 匹配。 And the same goes to Resources page also, even there i'm unable to get my array matched.资源页面也是如此,即使在那里我也无法匹配我的数组。

what is the best way to do the destructuring to make it work.. ?进行解构以使其工作的最佳方法是什么..?

   //DATA STRUCTURE
      const Data = [
  {
    Branch: "Electrical",
    id: "AA",
    Items: [
      {
        name: "LIGHT FITTINGS",
        id: "1",
        description: "A wide range of light fittings for your need",
        resources: [
          {
            id: "1a",
            title: "Bulb",
            description:
              "This is for all the latest LED light fittings, no matter who you are, where you’re from and what you’re up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",                  
          },
          {
            id: "2a",
            title: "Tube light",
            description:
              "This is for all the latest LED light fittings, no matter who you are, where you’re from and what you’re up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",                
          },
          {
            id: "3a",
            title: "Tube light 2nd",
            description:
              "This is for all the latest LED light fittings, no matter who you are, where you’re from and what you’re up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",
          }
        ]
      },
      {
        name: "SWITCH & SOCKETS",
        id: "2",
        description: "A wide range of Switches & sockets for your need",
        resources: [
          {
            id: "1b",
            title: "Switch",
            description:
              "This is for all the latest switches, no matter who you are, where you’re from and what you’re up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",
          },
          {
            id: "3b",
            title: "15A Switch + Socket",
            description:
              "This is for all the latest switches, no matter who you are, where you’re from and what you’re up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",
          },
          {
            id: "2b",
            title: "Socket",
            description:
              "This is for all the latest Sockets, no matter who you are, where you’re from and what you’re up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",
          }
        ]
      }
    ]
  }
];
export default Data;

//Product ID page //产品标识页

import React from "react";
import Data from "../Data";
import { Link, Route, useRouteMatch, Switch } from "react-router-dom";
import Resource from "./Resource";



export default function ProductId({ match }) {
    let { path, url } = useRouteMatch();

 const productItem = Data.Items.find(({ id }) => id === match.params.productId);
    return (
        <div>
            <h2>ProductId page</h2>
            <div>{productItem.name}</div>

                <ul>
                {productItem.resources.map((sub) => (
               <li key={sub.id}>
                    <Link to={`${url}/${sub.id}`}>{sub.title}</Link>
               </li>
            ))}
            </ul>

            <Route path={`${path}/:subId`} component={Resource} />

        </div>
     );
     }

As I mentioned in the comment section the main problem is Data is also an array and it does not have Items as a property as you expect.正如我在评论部分提到的,主要问题是Data也是一个数组,它没有您期望的Items作为属性。 Probably you need to loop through Data first then inside Items to find your id .可能您需要先循环遍历Data然后在Items中找到您的id

One solution can be to flatten your array first with .flatMap() then use .find() as before.一种解决方案是先使用.flatMap()将数组展平,然后像以前一样使用.find()

The flatMap() method first maps each element using a mapping function, then flattens the result into a new array. flatMap()方法首先使用映射函数映射每个元素,然后将结果展平到一个新数组中。 It is identical to a map() followed by a flat() of depth 1, but flatMap() is often quite useful, as merging both into one method is slightly more efficient.它与map()后跟一个深度为 1 的flat()相同,但flatMap()通常非常有用,因为将两者合并为一种方法效率更高。

See an example with one element from your array:请参阅包含数组中一个元素的示例:

 const Data = [{ Branch: "Electrical", id: "AA",Items: [{name: "LIGHT FITTINGS",id: "1",description: "A wide range of light fittings for your need",resources: [{id: "1a",title: "Bulb",description:"This is for all the latest LED light fittings, no matter who you are, where you're from and what you're up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",},{id:"2a",title: "Tube light",description:"This is for all the latest LED light fittings, no matter who you are, where you're from and what you're up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",},{id: "3a",title:"Tube light 2nd",description:"This is for all the latest LED light fittings, no matter who you are, where you're from and what you're up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",}]},{name: "SWITCH & SOCKETS",id: "2",description: "A wide range of Switches & sockets for your need",resources: [{id: "1b",title: "Switch",description:"This is for all the latest switches, no matter who you are, where you're from and what you're up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",},{id: "3b",title: "15A Switch + Socket",description:"This is for all the latest switches, no matter who you are, where you're from and what you're up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",},{id: "2b",title:"Socket",description:"This is for all the latest Sockets, no matter who you are, where you're from and what you're up to. Exclusive to ASOS, our universal brand is here for you, and comes in all our fit ranges: ASOS Curve, Tall, Petite and Maternity. Created by us, styled by you.",}]}]}] const routeId = '1'; const result = Data.flatMap(e => e.Items) .find(({id}) => id === routeId); console.log(result);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM