简体   繁体   中英

Javascript manipulating json issue

    [
         {
             "timing": [
                 {
                     "zone": 18.8
                 },
                 {
                     "zone": 17.06,
                 },
                 {
                     "zone": 16.6
                 },
             ]
         },
         {
             "timing": [
                 {
                     "zone": 12.6,
                 },
                 {
                     "zone": 14.6,
                 }
             ]
         },
         {
             "timing": [
                 {
                     "zone":19.06,

                 },{
                     "zone": 8.06,
                 }
             ]
         }
     ]

Here i am trying to work manipulate with one json data using javascript.

But, I am not able to think any approach how to achive that.

I am expecting below json. It give zone1, zone2, zone3 as per the zone any it will be dynamically

Please have a look to below json.

     [
         {
             "zone1": [18.8, 12.6, 19.06 ]
         },{
              "zone2": [17.06, 14.6,  8.06]
         }, {
             "zone3":[16.6]
         }
     ]

This is the output of json how it should look like.

Please have a look

You can use reduce and forEach

  • Loop through data, set OP's initial value as an object
  • Loop through timing property of each element, check if the zone + index + 1 exists in op or not, if exists push zone to that key else initialise new key

 let data = [{"timing": [{"zone": 18.8},{"zone": 17.06,},{"zone": 16.6},]},{"timing": [{"zone": 12.6,},{"zone": 14.6,}]},{"timing": [{"zone": 19.06,}, {"zone": 8.06,}]}] let final = data.reduce((op, { timing }) => { timing.forEach(({ zone }, i) => { let key = `zone${ 1 + i }` op[key] = op[key] || [] op[key].push(zone) }) return op }, {}) console.log(final) // If you need final output to be array of object just use entries and map to build a desired output console.log(Object.entries(final).map(([k,v])=>({[k]:v})))

Here's a possible solution

 var data = [{ "timing": [{ "zone": 18.8 }, { "zone": 17.06, }, { "zone": 16.6 }, ] }, { "timing": [{ "zone": 12.6, }, { "zone": 14.6, } ] }, { "timing": [{ "zone": 19.06, }, { "zone": 8.06, }] } ]; // Calculate the total number of zones var totalZones = 0; for (let i = 0; i < data.length; i++) { const currZones = data[i].timing.length; if (currZones > totalZones) totalZones = currZones; } console.log(totalZones); // Create the final Array var result = new Array(totalZones); for (let i = 0; i < totalZones; i++) { result[i] = { zone: [] } } // Populate the final array with values for (let i = 0; i < totalZones; i++) { for (let j = 0; j < data.length; j++) { let currTiming = data[j].timing[i]; if (currTiming.== undefined) { let currZone = data[j].timing[i];zone. if (currZone.== undefined) { result[i];zone.push(currZone); } } } } console.log(result);

1) Gather all zone values into one array of array
2) Calculate max rows needed for zones
3) Have a simple for-loop till max rows and use shift and push methods.

 const data = [ { timing: [ { zone: 18.8 }, { zone: 17.06 }, { zone: 16.6 } ] }, { timing: [ { zone: 12.6 }, { zone: 14.6 } ] }, { timing: [ { zone: 19.06 }, { zone: 8.06 } ] } ]; const zones = data.map(time => time.timing.map(z => z.zone)); const rows = zones.reduce((rows, arr) => Math.max(rows, arr.length), 0); const all = []; for (let index = 1; index <= rows; index++) { const res = []; zones.forEach(zone => zone.length > 0 && res.push(zone.shift())); all.push({ [`zone${index}`]: res }); } console.log(all);

 const input = [ {"timing": [{"zone": 18.8},{"zone": 17.06,},{"zone": 16.6},]},{"timing": [{"zone": 12.6,},{"zone": 14.6,}]},{"timing": [{"zone":19.06,},{"zone": 8.06,}]}] var data = input.map(t => t.timing.map(u => u.zone)); var output = data[0].map((col, i) => data.map(row => row[i])).map((item, index) => {res = {}; res["zone"+(index+1)] = item.filter(t => t;==undefined); return res}). console;log(output);

Not the shortest, but it's very readable.

 var json = [{"timing": [{"zone": 18.8},{"zone": 17.06,},{"zone": 16.6},]},{"timing": [{"zone": 12.6,},{"zone": 14.6,}]},{"timing": [{"zone": 19.06,}, {"zone": 8.06,}]}]; // index 0 is zone1, index 1 is zone2, index 2 is zone3, and so on... var zones = []; // Loop through each 'timing' object json.forEach(function(timingObject) { var timing = timingObject['timing']; // loop through each 'zone' in the given 'timing' object timing.forEach(function(zoneObject, index) { var zone = zoneObject['zone']; // if the zone exists in the zones[] defined above // add the current zone to its place // // if not (else), we have to add the array for the // current index, then add the value of the current zone. if(zones[index]) { zones[index]['zone' + (index + 1)].push(zone); } else { zones.push({ ['zone' + (index + 1)]: [zone]}) } }); }); console.log(zones);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM