代码之家  ›  专栏  ›  技术社区  ›  tjugg

javascript nested.map()返回多个数组

  •  1
  • tjugg  · 技术社区  · 8 年前

    鉴于我有一个 JSON 结构如下:

    {
        "firstData": [{
            "secondData": [{
                "thirdData": [{
                    "value": "whatever"
                }]
            }]
        }]
    }
    

    我需要从地图上 thirdData value === "whatever"

    所以我正在做

     const result = firstData.map(first => {
                return first.secondData.map(second => {
                    return second.thirdData.map(third => {
                        return third.value === 'whatever';
                    });
                });
            });
    

    这有点好,但结果是另一个深度嵌套的数组(如 [ [ [ {results..} ] ] ]) . 我知道我可以用其他方法把它展平成一个数组,但我觉得我错过了使用 .map() . 如何将此结果修改为包含以下值的单个数组 三数据 我想要的价值在哪里?

    所需的结果是 三数据 物体:

    [{ value: 'whatever'}, ... {n}]
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   T.J. Crowder    8 年前

    如果你想要一个简单的结果,这不是 map . 简单的解决方案就是使用一个您关闭并推到的数组:

    const result = [];
    firstData.forEach(first => {
        return first.secondData.forEach(second => {
            result.push(...second.thirdData.filter(third => third.value === 'whatever'));
        });
    });
    

    对您所提供的最少数据进行稍微扩展的实时示例:

    const data = {
        "firstData": [{
                "secondData": [{
                    "thirdData": [{
                            "value": "whatever",
                            "label": "third #1.1"
                        },
                        {
                            "value": "whatever",
                            "label": "third #1.2"
                        },
                        {
                            "value": "unrelated",
                            "label": "unrelated"
                        }
                    ]
                }]
            },
            {
                "secondData": [{
                    "thirdData": [{
                            "value": "another unrelated"
                        },
                        {
                            "value": "whatever",
                            "label": "third #2"
                        }
                    ]
                }]
            }
        ]
    };
    const result = [];
    data.firstData.forEach(first => {
        return first.secondData.forEach(second => {
            result.push(...second.thirdData.filter(third => third.value === 'whatever'));
        });
    });
    console.log(result);
    .as-console-wrapper {
      max-height: 100% !important;
    }

    注意 filter 上 thirdData 并使用扩展符号将数据推送到 result .

    假设您希望输入来自 三数据 那有 .value === 'whatever' 而不是 true / false . 如果你想要 真 / 假 相反,改变它 滤波器 到 地图 .

    或 for-of 当量:

    const result = [];
    for (const first of firstData) {
        for (const second of first.secondData) {
            result.push(...second.thirdData.filter(third => third.value === 'whatever'));
        }
    }
    

    对您所提供的最少数据进行稍微扩展的实时示例:

    const data = {
        "firstData": [{
                "secondData": [{
                    "thirdData": [{
                            "value": "whatever",
                            "label": "third #1.1"
                        },
                        {
                            "value": "whatever",
                            "label": "third #1.2"
                        },
                        {
                            "value": "unrelated",
                            "label": "unrelated"
                        }
                    ]
                }]
            },
            {
                "secondData": [{
                    "thirdData": [{
                            "value": "another unrelated"
                        },
                        {
                            "value": "whatever",
                            "label": "third #2"
                        }
                    ]
                }]
            }
        ]
    };
    const result = [];
    for (const first of data.firstData) {
        for (const second of first.secondData) {
            result.push(...second.thirdData.filter(third => third.value === 'whatever'));
        }
    }
    console.log(result);
    .作为控制台包装{
    最大高度:100%!重要的;
    }

    (关于 滤波器 / 地图 )

    和所有的数组操作一样,您可以将其放入 reduce ,我保证您将主要通过以下方式获得答案: 减少 但是没有充分的理由使用 减少 在这里。

    const result = firstData.reduce((result, first) => {
        return first.secondData.reduce((result, second) => {
            result.push(...second.thirdData.filter(third => third.value === 'whatever'));
            return result;
        }, result);
    }, []);
    

    不过,这也没有什么好的理由。只是更复杂了。

    对您所提供的最少数据进行稍微扩展的实时示例:

    const data = {
        "firstData": [{
                "secondData": [{
                    "thirdData": [{
                            "value": "whatever",
                            "label": "third #1.1"
                        },
                        {
                            "value": "whatever",
                            "label": "third #1.2"
                        },
                        {
                            "value": "unrelated",
                            "label": "unrelated"
                        }
                    ]
                }]
            },
            {
                "secondData": [{
                    "thirdData": [{
                            "value": "another unrelated"
                        },
                        {
                            "value": "whatever",
                            "label": "third #2"
                        }
                    ]
                }]
            }
        ]
    };
    const result = data.firstData.reduce((result, first) => {
        return first.secondData.reduce((result, second) => {
            result.push(...second.thirdData.filter(third => third.value === 'whatever'));
            return result;
        }, result);
    }, []);
    console.log(result);
    .作为控制台包装{
    最大高度:100%!重要的;
    }

    (关于 滤波器 / 地图 )

        2
  •  2
  •   Pranav C Balan Ansh Takalkar    8 年前

    你可以使用 Array#reduce 用于减少为单个值(在本例中为单个数组)和 Array#forEach 用于迭代嵌套数组。

    const data = {
      "firstData": [{
        "secondData": [{
          "thirdData": [{
            "value": "whatever"
          }]
        }]
      }]
    }
    
    const result = data.firstData.reduce((arr, first) => {
      // iterate over the second level array
      first.secondData.forEach(second => {
        // iterate over the third level array
        second.thirdData.forEach(third => {
    
          // push the value into the result array,
          // change here, in case you want the value 
          //arr.push(third.value === 'whatever');
          
          // in case you need the object then do it like
          if(third.value === 'whatever') arr.push(third);
    
        });
      });
      // return the array reference for the next iteration
      return arr;
    
      // set the initial value as an array for the result
    }, []);
    
    console.log(result);
    推荐文章