无法在 forEach 函数中编写 map 函数?

Failing to write map function inside forEach function?

我正在尝试forEach json中的值,并使用里面的map函数将想要的值推送到数组中。

[
    {
        "detail": {
            "id": 89,
            "content": "123",
            "type": "one",
            "company": "minsu",
            "certificationNum": "minsu"
        },
        "ingredientList": [
            {
                "id": 161,
                "content": "32523523",
                "functionalList": [
                    {
                        "id": 129,
                        "valueUnit": "mg"
                    }
                ]
            },
            {
                "id": 162,
                "content": "162",
                "functionalList": [
                    {
                        "id": 130,
                        "valueUnit": "cm"
                    }
                ]
            }
        ]
    },
    {
        "detail": {
            "id": 91,
            "content": "332",
            "type": "two",
            "company": "sss",
            "certificationNum": "1123-522"
        },
        "ingredientList": [
            {
                "id": 164,
                "content": "hi",
                "functionalList": [
                    {
                        "id": 132,
                        "valueUnit": "kg"
                    },
                    {
                        "id": 133,
                        "valueUnit": "g"
                    }
                ]
            },
            {
                "id": 165,
                "content": "succes",
                "functionalList": [
                    {
                        "id": 134,
                        "valueUnit": "fie"
                    },
                    {
                        "id": 135,
                        "valueUnit": "fy"
                    }
                ]
            }
        ]
    }
]

很简单json,
我想把json个数据值push到一个数组中,把它们作为这些值。



[
    {
        "content": "123",
        "type": "one",
        "certificationNum": "minsu",
        "functionalList": {
            {
                "id": 129,
                "valueUnit": "mg"
            },
            {
                "id": 130,
                "valueUnit": "cm"
            }
        }
    },
    {
        "content": "332",
        "type": "two",
        "certificationNum": "1123-522",
        "functionalList": {
            {
                "id": 132,
                "valueUnit": "kg"
            },
            {
                "id": 133,
                "valueUnit": "g"
            },
            {
                "id": 134,
                "valueUnit": "fie"
            },
            {
                "id": 135,
                "valueUnit": "fy"
            }
        }
    }
]
    let SeedDetailArray = new Array();


   SeedValue.forEach(function(id: any) {
                Object.keys(id).forEach(function(props) {
                    SeedDetailArray.push({
                      content: id[props]["content"],
                      type: id[props]["type"],
                      certificationNum: id[props]["certificationNum"],
                      functionalList: id[props]["functionalList"],
                    });
                });
            })

console.log(种子值);


[
    {
        "content": "123",
        "type": "one",
        "certificationNum": "minsu",
    
        
    },
    {
        "content": "332",
        "type": "two",
        "certificationNum": "1123-522",
       
        
    }
]

我用 detail forEach 语句将 Seed Value json 放入 Seed Detail Array,但我不知道如何将值放入 functionList

如何创建这样的 json?


[
    {
        "content": "123",
        "type": "one",
        "certificationNum": "minsu",
        "functionalList": {
            {
                "id": 129,
                "valueUnit": "mg"
            },
            {
                "id": 130,
                "valueUnit": "cm"
            }
        }
    },
    {
        "content": "332",
        "type": "two",
        "certificationNum": "1123-522",
        "functionalList": {
            {
                "id": 132,
                "valueUnit": "kg"
            },
            {
                "id": 133,
                "valueUnit": "g"
            },
            {
                "id": 134,
                "valueUnit": "fie"
            },
            {
                "id": 135,
                "valueUnit": "fy"
            }
        }
    }
]

您可以使用 tmp 数组来保存 functionalList

let SeedDetailArray = new Array();


SeedValue.forEach(function(id) {
    let tmp = []
    id.ingredientList.forEach(v1=>{
        v1.functionalList.forEach(v2=>{
            tmp.push(v2)
        })
    })
    Object.keys(id).forEach(function(props) {
        SeedDetailArray.push({
          content: id[props]["content"],
          type: id[props]["type"],
          certificationNum: id[props]["certificationNum"],
          functionalList: tmp,
        });
    });
})
SeedValue.forEach(function(id: any) {
                Object.keys(id).forEach(function(props) {
                    // YOUR CODE
                });
            })

您正在遍历键,这将为每个对象 return 两个键 detailingredientList。因此,您的代码将生成一个包含 undefined 内容的对象。

我使用了带有对象解构的映射并使用临时数组来获取功能列表。

let a = [
  {
    detail: {
      id: 89,
      content: "123",
      type: "one",
      company: "minsu",
      certificationNum: "minsu",
    },
    ingredientList: [
      {
        id: 161,
        content: "32523523",
        functionalList: [
          {
            id: 129,
            valueUnit: "mg",
          },
        ],
      },
      {
        id: 162,
        content: "162",
        functionalList: [
          {
            id: 130,
            valueUnit: "cm",
          },
        ],
      },
    ],
  },
  {
    detail: {
      id: 91,
      content: "332",
      type: "two",
      company: "sss",
      certificationNum: "1123-522",
    },
    ingredientList: [
      {
        id: 164,
        content: "hi",
        functionalList: [
          {
            id: 132,
            valueUnit: "kg",
          },
          {
            id: 133,
            valueUnit: "g",
          },
        ],
      },
      {
        id: 165,
        content: "succes",
        functionalList: [
          {
            id: 134,
            valueUnit: "fie",
          },
          {
            id: 135,
            valueUnit: "fy",
          },
        ],
      },
    ],
  },
];

console.log(a.map(({ detail: { content, type, certificationNum }, ingredientList }) => {
let functionalList = ingredientList.map(({ functionalList }) => functionalList).flat();
  return {
    content: content,
    type: type,
    certificationNum: certificationNum,
    functionalList,
  };
}));