简体   繁体   English

.map() function 返回一个 arrays 数组而不是对象数组

[英].map() function is returning an array of arrays instead of array of objects

Does anybody know why this filterData map function is有人知道为什么这个filterData map function 是

returning an array of arrays instead of array of objects?返回 arrays 数组而不是对象数组?

I am using a map() function inside another map() so basically I am trying to iterate first map function over the big array and afterwards run it inside the child array.我在另一个 map() 中使用 map() function 所以基本上我试图先在大数组上迭代 map function 然后在子数组中运行它。

I just want to return an simple object only wit the data that I select in the second map object.我只想返回一个简单的 object,仅返回第二个 map object 中的数据 select。

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
            };
          })
        )
      });
    });
  }

and this is the json structure that I want to iterate, map cannot run into the second array from object.这是我要迭代的json结构,map不能从object跑到第二个数组。

[
    {
        "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
    }
]

Using the combination of flatMap , map and destructuring can simplify something like below.使用flatMapmapdestructuring的组合可以简化如下所示的内容。 (PS. Just interpreted the data in your case, Update your data model if you still an issue) (PS. 刚刚解读了你的数据,如果还有问题请更新你的数据model)

 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)

The solution may be one possible solution to achieve the below described output structure / format:该解决方案可能是实现以下描述的 output 结构/格式的一种可能解决方案:

  id: nested0.id,         // outer-array "Id" prop
  name: nested0.serve,    // outer-array "serve" prop
  date: nested1.name      // inner-array "name" prop

Code Snippet代码片段

 // method to obtain the array of transformed objects const transformData = arr => ( arr.flatMap( // iterate the outer-array using "flatMap()" ({ Id, serve, childNested }) => ( // de-structure to directly access props childNested.map( // iterate over inner-array "childNested" ({ name }) => ({ // de-structure to directly access "name" prop id: Id, // structure the desired output object name: serve, date: name }) ) ) ) // implicit "return" will send the result of "flatMap()" ); const rawData = [{ "Id": "tryuk34io98i", "src": "pl.net", "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 } ]; console.log(transformData(rawData));

Explanation解释

Inline comments in the snippet are added.添加了片段中的内嵌注释。

Because you are using the nested Map Function, First Map returns a second map. Both Maps will return an array.因为您使用的是嵌套的 Map Function,第一个 Map 返回第二个 map。两个 Maps 都会返回一个数组。

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

相关问题 JavaScript地图返回对象数组,而不是ojbect - JavaScript map returning array of objects instead of ojbect Function 返回 object 而不是数组,无法.Map - Function returning object instead of Array, unable to .Map map function 用于对象(而不是数组) - map function for objects (instead of arrays) 在对 arrays 的 js object 进行排序时,它返回键数组而不是对象 - while sorting js object of arrays, it is returning keys array instead of objects Javascript 递归 function 正在返回多个结果 arrays 而不是最终数组在循环 Z70ECD11C8D7D7BBBB 后具有所有对象 - Javascript recursive function is returning multiple result arrays instead of a final array having all the objects after looping over a JSON file lodash map返回对象数组 - lodash map returning array of objects Javascript正在传递对象数组而不是数组数组 - Javascript is passing an Array of Objects instead of an Array of Arrays Map函数返回带索引而不是字符串值的数组 - Map function returning array with index instead of string value 如何 map arrays 的 arrays 数组与反应中的对象 - How to map an array of arrays of arrays with objects in react .map返回数组而不是数字数组中的字符串 - .map returning string in array instead of array of numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM