简体   繁体   English

将本机数组反应到节列表

[英]React Native Array to Section List

I'm trying to format some data into a SectionList on my react native app, but I'm running into some issues with getting it working.我正在尝试将一些数据格式化为我的反应本机应用程序上的 SectionList,但我遇到了一些让它工作的问题。 Essentially, I have a list of ingredients that are connected to their recipeId and some other information.本质上,我有一个与他们的 recipeId 和一些其他信息相关的成分列表。 I'm using lodash to sort the data into arrays based on the recipeName and the results look like this:我正在使用 lodash 根据 recipeName 将数据排序到 arrays 中,结果如下所示:

Object {
  "Blueberry Chaffles": Array [
    Object {
      "ingredient": "1/2 Cup Mozzarella Cheese (shredded and chopped into bits)",
      "recipeId": 749,
      "recipeName": "Blueberry Chaffles",
      "uniqueId": "7491/2 Cup Mozzarella Cheese (shredded and chopped into bits)",
    },
  ],
  "Everything Chaffle with Cream Cheese and Salmon": Array [
    Object {
      "ingredient": "CHAFFLES",
      "recipeId": 750,
      "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
      "uniqueId": "750CHAFFLES",
    },
    Object {
      "ingredient": "1 Egg",
      "recipeId": 750,
      "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
      "uniqueId": "7501 Egg",
    },
    Object {
      "ingredient": "2 Tsp Everything Bagel Seasoning",
      "recipeId": 750,
      "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
      "uniqueId": "7502 Tsp Everything Bagel Seasoning",
    },
    Object {
      "ingredient": "REMAINING INGREDIENTS",
      "recipeId": 750,
      "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
      "uniqueId": "750REMAINING INGREDIENTS",
    },
    Object {
      "ingredient": "2 Oz Smoked Salmon",
      "recipeId": 750,
      "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
      "uniqueId": "7502 Oz Smoked Salmon",
    },
  ],
}

which is the lodash output of:这是 lodash output 的:

let array = _.groupBy(recipesFav, o => o.recipeName)

The issue I'm running into now is trying to display this data in a sectionList.我现在遇到的问题是尝试在 sectionList 中显示这些数据。 I've followed the documentation on the react website, but nothing seems to work.我已经按照反应网站上的文档进行操作,但似乎没有任何效果。 Essentially I want to be able to display the Recipe Name as a sectionlist heading and display the ingredients that correspond to this recipe underneath it.本质上,我希望能够将配方名称显示为部分列表标题,并在其下方显示与该配方相对应的成分。 Any help would be greatly appreciated!任何帮助将不胜感激!

Edit: Current SectionList code that is not working:编辑:当前 SectionList 代码不起作用:

<SectionList
                    sections={this.state.shopping_list}
                    keyExtractor={(item, index) => item + index}
                    renderItem={({ item }) => <Item title={recipeName} />}
                    renderSectionHeader={({ section: { recipeName } }) => (
                        <Text>{recipeName}</Text>
                    )}
                />

Each section should be organized as key value pairs in an array, such as每个部分都应组织为数组中的键值对,例如

Data = [
{
    Title: "Blueberry Chaffles",
    Info: [
        {
            "ingredient": "1/2 Cup Mozzarella Cheese (shredded and chopped into bits)",
            "recipeId": 749,
            "recipeName": "Blueberry Chaffles",
            "uniqueId": "7491/2 Cup Mozzarella Cheese (shredded and chopped into bits)",
        },
    ]
},
{
    Title: "Everything Chaffle with Cream Cheese and Salmon",
    Info: [
        {
            "ingredient": "CHAFFLES",
            "recipeId": 750,
            "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
            "uniqueId": "750CHAFFLES",
        },
        {
            "ingredient": "1 Egg",
            "recipeId": 750,
            "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
            "uniqueId": "7501 Egg",
        },
        {
            "ingredient": "2 Tsp Everything Bagel Seasoning",
            "recipeId": 750,
            "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
            "uniqueId": "7502 Tsp Everything Bagel Seasoning",
        },
        {
            "ingredient": "REMAINING INGREDIENTS",
            "recipeId": 750,
            "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
            "uniqueId": "750REMAINING INGREDIENTS",
        },
        {
            "ingredient": "2 Oz Smoked Salmon",
            "recipeId": 750,
            "recipeName": "Everything Chaffle with Cream Cheese and Salmon",
            "uniqueId": "7502 Oz Smoked Salmon",
        },
    ],
}

]; ];

you can try this.你可以试试这个。

// Object.keys is used to iterate over object keys with this you can form an array. // Object.keys 用于迭代 object 键,您可以使用它形成一个数组。

const section_list_data = Object.keys(response_object).map(key => ({ title: key, data: response_object[key] }));常量 section_list_data = Object.keys(response_object).map(key => ({ title: key, data: response_object[key] }));

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

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