soubhagya pradhan
soubhagya pradhan

Reputation: 547

JavaScript json manipulation issue

     [
            {
              "timing": [
                {
                  "zone": [
                    {
                      "stepName": "Pick Place Tray",
                      "timeInfo": [
                        "6.0"
                      ]
                    },
                    {
                      "stepName": "Pick Place Back Bezel",
                      "timeInfo": [
                        "3.3"
                      ]
                    },
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                }
              ],
            },
            {
              "zone": [
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                },
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                }
              ],
            },
            {
              "zone": [
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                },
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                }
              ],
            },
          ]
        },
        {
          "timing": [
            {
              "zone": [
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                },
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                }
              ],

            },
            {
              "zone": [
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                },
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                }
              ],
            }
          ]
        },
        {
          "timing": [
            {
              "zone": [
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                },
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                }
              ],

            },{
              "zone": [
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                },
                {
                  "stepName": "Pick Place Tray",
                  "timeInfo": [
                    "6.0"
                  ]
                },
                {
                  "stepName": "Pick Place Back Bezel",
                  "timeInfo": [
                    "3.3"
                  ]
                }
              ],
            }
          ]
        }
      ]

I am trying to manipulate the above data, but it doesn't give the expected result:

for(let i of data){
        for (let j of i["timing"]){
        let zone_count = 0
        for(let k of j["zone"]){
            if(k["timeInfo"].length){
                zone_count+=parseFloat(k["timeInfo"][0])
            }
        }
    }
}

I expect the below result. It will add all first elements of the timeInfo array in each object.

Please have a look to below json.

    [
         {
             "timing": [
                 {
                     "zone": 18.8
                 },
                 {
                     "zone": 18.06,
                 },
                 {
                     "zone": 18.6
                 },
             ]
         },
         {
             "timing": [
                 {
                     "zone": 18.6,
                 },
                 {
                     "zone": 18.6,
                 }
             ]
         },
         {
             "timing": [
                 {
                     "zone":18.06,

                 },{
                     "zone": 18.06,
                 }
             ]
         }
     ]

In all zone objects it should sum of all the first values of timeInfo arrays.

Upvotes: 0

Views: 52

Answers (2)

trincot
trincot

Reputation: 350961

You never create your data structure. The only output you generate is a sum in one variable.

You'll need to map your data, and nested data, and perform the sum there.

Here is a functional approach:

let result = data.map(({timing}) => ({
    timing: timing.map(({zone}) => ({
        zone: zone.reduce((sum, {timeInfo}) => sum + (timeInfo && +timeInfo[0]), 0)
    }))
}));

If you have other fields besides timing that you want to retain as they are, then use ...rest like here:

let result = data.map(({timing, ...rest}) => ({
    timing: timing.map(({zone}) => ({
        zone: zone.reduce((sum, {timeInfo}) => sum + (timeInfo && +timeInfo[0]), 0)
    })),
    ...rest
}));

Upvotes: 1

iamkhush
iamkhush

Reputation: 2592

the zone_count is not assigned to anything. you need to assign the zone_count back to the array

Upvotes: 0

Related Questions