繁体   English   中英

嵌套阵列内的 map 阵列

[英]map array inside nested array

我有一个带有另一个嵌套数组的数组,我需要 map 过来。 我想我几乎得到了它,但我没有取回该数据,只能看到它的 object。 查看新索引,我可以看到它正在循环 object 数组中的对象数量。

这是我目前拥有的:

class Menu extends Component {
    constructor(props) {
        super(props);
        const arrayItems = [this.props.items.edges]
        arrayItems.map((currentItem, index) => {
            console.log("The current iteration is: " + index)
            console.log("The current item is: " + currentItem)
            return currentItem.map((newCurrentItem, newIndex) => {
                console.log("The NEW current iteration is: " + newIndex)
                console.log("The NEW current item is: " + newCurrentItem)
                return newCurrentItem
            })
        })

...
}

这是我在控制台中看到的屏幕截图,看起来很有希望:

在此处输入图像描述

有人可以指出我正确的方向吗?

实际上, currentItem在您的代码中等于this.props.items.edges ,因此您可以只使用 map this.props.items.edges

newCurrentItem显示为[object Object]是因为 [object Object] 是什么意思? (JavaScript)

所以你可以像往常一样从CurrentItem中获取数据。

您的代码可以简化为:

class Menu extends Component {
    constructor(props) {
        super(props);
        const arrayItems = this.props.items.edges
        return arrayItems.map((currentItem) => {
            return currentItem.id // or other attributes you what
        })

...
}

在我看到你的命令在 state 按字母顺序排序项目之后,我相信 map 节点值应该是你想要的。

  const newCurrentItem = [{ node: "abc" }, { node: "def" }];

  // [Object, Object]
  console.log(newCurrentItem);

  // ["abc", "def"]
  console.log(
    newCurrentItem.map((data) => {
      return data.node;
    })
  );

  const newCurrentItem2 = [{ node: { aaa: "bbb" } }, { node: { aaa: "yyy" } }];

  // [Object, Object]
  console.log(newCurrentItem2);

  // ["bbb", "yyy"]
  console.log(
    newCurrentItem2.map((data) => {
      return data.node.aaa;
    })
  );

码沙箱

尝试arrayItems.forEach(currentItem => { //do something })你也可以使用.map在这种情况下你需要提取像@eux说的任何属性的值

暂无
暂无

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

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