简体   繁体   English

在嵌套数组中映射/循环反应

[英]Map/Loop over nested array in react

I have tried a few ways now and it only confuses me more, probably still above my knowledge but trying to get the the following to work: 我现在已经尝试了几种方法,但这只会使我更加困惑,可能仍然超出我的知识范围,但是试图使以下各项起作用:

I am pulling in product data from woocommerce and am trying to loop/map over the variations. 我从woocommerce提取产品数据,并试图循环/映射变化。 The end goal is to have a dropdown (or radio buttons or anything really) for each variation as in: 最终目标是为每个变体创建一个下拉列表(或单选按钮或其他任何东西),如下所示:

Size: SML 尺码:SML

Color: red blue green 颜色:红色蓝色绿色

Here is the data I get from woocommerce and below, my attempt of getting it to work. 这是我从woocommerce及以下获得的数据,我试图使其工作。

Data: 数据:

{
  "data": {
    "allWcProducts": {
      "edges": [
        {
          "node": {
            "variations": [
              {
                "id": 14,
                "attributes": [
                  {
                    "name": "color",
                    "option": "red"
                  },
                  {
                    "name": "size",
                    "option": "S"
                  }
                ]
              },
              {
                "id": 15,
                "attributes": [
                  {
                    "name": "color",
                    "option": "red"
                  },
                  {
                    "name": "size",
                    "option": "M"
                  }
                ]
              },
              {
                "id": 16,
                "attributes": [
                  {
                    "name": "color",
                    "option": "red"
                  },
                  {
                    "name": "size",
                    "option": "L"
                  }
                ]
              },
              {
                "id": 17,
                "attributes": [
                  {
                    "name": "color",
                    "option": "blue"
                  },
                  {
                    "name": "size",
                    "option": "S"
                  }
                ]
              },
              {
                "id": 18,
                "attributes": [
                  {
                    "name": "color",
                    "option": "blue"
                  },
                  {
                    "name": "size",
                    "option": "M"
                  }
                ]
              },
              {
                "id": 19,
                "attributes": [
                  {
                    "name": "color",
                    "option": "blue"
                  },
                  {
                    "name": "size",
                    "option": "L"
                  }
                ]
              },
              {
                "id": 20,
                "attributes": [
                  {
                    "name": "color",
                    "option": "green"
                  },
                  {
                    "name": "size",
                    "option": "S"
                  }
                ]
              },
              {
                "id": 21,
                "attributes": [
                  {
                    "name": "color",
                    "option": "green"
                  },
                  {
                    "name": "size",
                    "option": "M"
                  }
                ]
              },
              {
                "id": 22,
                "attributes": [
                  {
                    "name": "color",
                    "option": "green"
                  },
                  {
                    "name": "size",
                    "option": "L"
                  }
                ]
              }
            ]
          }
        }
      ]
    }
  }
}

My attempt that currently only outputs: 我目前仅输出的尝试:

color 颜色

red 红色

M 中号

.....
    const post = this.props.data.wcProducts
......
{
  post.variations.map((variations, i) => (
    <div key={`${post.variations} ${i}`}>
      <p>{post.variations[i].attributes[0].name}</p>

      {post.variations[i].attributes.map((wcProducts, i) => (
        <div key={`${post.variations} ${i}`}>
          <p>{post.variations[i].attributes[i].option}</p>
        </div>
      ))}
    </div>
  ));
}

Tried switching around pretty much everything and it just gets more confusing to me so any input would be much appreciated. 尝试切换几乎所有内容,这让我感到更加困惑,因此任何输入将不胜感激。

Here is one way to do this: 这是执行此操作的一种方法:

 const data = { "allWcProducts": { "edges": [ { "node": { "variations": [ { "id": 14, "attributes": [ { "name": "color", "option": "red" }, { "name": "size", "option": "S" } ] }, { "id": 15, "attributes": [ { "name": "color", "option": "red" }, { "name": "size", "option": "M" } ] }, { "id": 16, "attributes": [ { "name": "color", "option": "red" }, { "name": "size", "option": "L" } ] }, { "id": 17, "attributes": [ { "name": "color", "option": "blue" }, { "name": "size", "option": "S" } ] }, { "id": 18, "attributes": [ { "name": "color", "option": "blue" }, { "name": "size", "option": "M" } ] }, { "id": 19, "attributes": [ { "name": "color", "option": "blue" }, { "name": "size", "option": "L" } ] }, { "id": 20, "attributes": [ { "name": "color", "option": "green" }, { "name": "size", "option": "S" } ] }, { "id": 21, "attributes": [ { "name": "color", "option": "green" }, { "name": "size", "option": "M" } ] }, { "id": 22, "attributes": [ { "name": "color", "option": "green" }, { "name": "size", "option": "L" } ] } ] } } ] } } const getOptions = data => { const result = data.allWcProducts.edges.reduce((r,{node}) => node.variations.forEach(({attributes}) => attributes.forEach(({name, option}) => r[name] ? r[name].add(option) : r[name] = new Set([option]))) || r, {}) return Object.entries(result).reduce((r, c) => (r[c[0]] = [...c[1]]) && r, {}) } console.log(getOptions(data)) 

It will dynamically create the array of the options and the main idea here is the use of the ES6 reduce and Set which takes care of keeping the values unique. 它将动态创建选项的数组,这里的主要思想是使用ES6 reduceSet来保持值的唯一性。

The last reduce is due to the results coming in a Set form and we are converting them to arrays. 最后一个reduce是由于结果以Set形式出现,我们将它们转换为数组。

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

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