Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Failing to write map function inside forEach function?

I am trying to forEach the values ​​in json and push the desired values ​​to the array by using the map function inside.

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

It’s a simple json,
I want to push the json data values ​​to an array and put them as these values.



[
    {
        "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(SeedValue);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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

I put Seed Value json in Seed Detail Array with detail forEach statement, but I don’t know how to put the value in functionList.

How do I create json like this?


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

>Solution :

you can use a tmp array to hold the 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,
        });
    });
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading