0

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


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

2 Answers 2

1

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,
        });
    });
})
Sign up to request clarification or add additional context in comments.

Comments

1
SeedValue.forEach(function(id: any) {
                Object.keys(id).forEach(function(props) {
                    // YOUR CODE
                });
            })

You are iterating through keys and that'll return two keys for each object detail and ingredientList. So your code will produce an object with undefined content.

I have used a map with object destructure and used a temp array to get the 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).flat();
  return {
    content: content,
    type: type,
    certificationNum: certificationNum,
    functionalList,
  };
}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.