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

未能在forEach函数中写入映射函数?

  •  0
  • minsu  · 技术社区  · 2 年前

    我试图在json中forEach值,并通过使用内部的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数据值推送到一个数组中,并将它们作为这些值。

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

    安慰log(种子值);

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

    我把 Seed Value json 在里面 Seed Detail Array 具有 detail forEach语句,但我不知道如何将值放入 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"
                }
            }
        }
    ]
    
    2 回复  |  直到 2 年前
        1
  •  1
  •   zhj    2 年前

    您可以使用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,
            });
        });
    })
    
        2
  •  1
  •   mc-user    2 年前
    SeedValue.forEach(function(id: any) {
                    Object.keys(id).forEach(function(props) {
                        // YOUR CODE
                    });
                })
    
    

    您正在遍历键,这将为每个对象返回两个键 detail ingredientList .所以你的代码会生成一个 undefined 所容纳之物

    我使用了一个带有对象分解结构的映射,并使用了一个临时数组来获取functionalList。

    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);
      return {
        content: content,
        type: type,
        certificationNum: certificationNum,
        functionalList,
      };
    }));