javascript - .map() 函数返回一个数组数组而不是对象数组

标签 javascript arrays reactjs ecmascript-6 axios

有人知道为什么这个filterData map函数是

返回一个数组而不是对象数组?

我在另一个 map() 中使用了 map() 函数,所以基本上我试图在大数组上迭代第一个 map 函数,然后在子数组中运行它。

我只想返回一个仅包含我在第二个 map 对象中选择的数据的简单对象。

function apiCall() {
    const promises = urls.map((url) => axios.get(url, { headers }));

    Promise.all(promises).then((responses) => {
      let data = [];
      let filterData;
      responses.forEach((response) => {
        data = data.concat(response.data);

        filterData = data.map((nested0) => 
          nested0.childNested.map((nested1) => {
            return {
              id: nested0.id,
              name: nested0.serve,
              date: nested1.name
            };
          })
        )
      });
    });
  }

这是我要迭代的json结构,map不能 从对象运行到第二个数组。

[
    {
        "Id": "tryuk34io98i",
        "src": "planet",
        "gwt": {
            "gwtId": 918,
            "name": "Request"
        },
        "serve": "Transit1",
        "childNested": [
            {
                "name": "xxl",
                "version": "001",
                "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                "solved": {
                    "id": "tik",
                    "name": "face",
                    "isOn": "false"
                },
                "externalRef": [
                    {
                        "type": "eight",
                        "uri": "git"
                    },
                    {
                        "type": "two",
                        "uri": "git"
                    }
                ],
                "project": {
                    "name": "InProgress",
                    "version": "1",                    
                    "active": true
                },
                "used": 0,
                "internal": false
            }
        ],
        "affe": 0
    },
    {
        "Id": "987ytrdfghv",
        "src": "Space",
        "gwt": {
            "gwt": 918,
            "name": "Request"
        },
        "serve": "Transit",
        "childNested": [
            {
                "name": "xxs",
                "version": "02",
                "description": "Nullam sed lorem nec sem lobortis porta. Morbi vitae lorem velit.",
                "solved": {
                    "id": "tok",
                    "name": "back face",
                    "isOn": true
                },
                "externalRef": [
                    {
                        "type": "one",
                        "uri": "http"
                    },
                    {
                        "type": "two",
                        "uri": "http"
                    }
                ],
                "project": {
                    "name": "Fail",
                    "version": "1.1",
                    "active": false
                },
                "used": 0,
                "internal": false
            }
        ],
        "affe": 0
    }
]

最佳答案

使用 flatMapmapdestructuring 的组合可以简化如下所示的内容。 (PS。刚刚解释了您案例中的数据,如果您仍然存在问题,请更新您的数据模型)

const responses = [
  {
    data: {
      id: "123",
      capacity: 20,
      childNested: [{ date: { name: "foo1" } }, { date: { name: "foo2" } }],
    },
  },
  {
    data: {
      id: "456",
      capacity: 40,
      childNested: [{ date: { name: "bar" } }],
    },
  },
];

const output = responses.flatMap(({ data }) =>
  data.childNested.map(({ date: { name } }) => ({
    id: data.id,
    name: data.capacity,
    date: name,
  }))
);

console.log(output)

关于javascript - .map() 函数返回一个数组数组而不是对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71838363/

相关文章:

javascript - 有没有办法告诉网络浏览器继续 "stuck"脚本?

javascript - 为 Sublime Text 3 定制 ternjs

ios - 使用 Swift 从 for 循环中查找 Firebase 键的 nil

arrays - SwiftUI watchOS 如何在单个 View 中将数组内容显示为 subview ?

reactjs - styled-components:使用额外的样式扩展现有组件

javascript - 这是根据 angularjs 中的根组织显示和隐藏 html 的代码的好方法吗

Python 将大型 numpy 数组转换为 pandas 数据框

javascript - 由于 SSR,Reactotron 无法与 NextJS 一起使用

javascript - 在 React 中通过 props 或 state 更新样式时如何避免重新渲染子项?

javascript - 网页无法正确加载: '"JSON"is undefined' error