简体   繁体   中英

How to restructure an object using javascript

I would like to restructure my object like below. Basically, i want to remove a nesting from my object. How to achieve that using js.

Actual:

 var a =    [ 
    { clickedEvents:
         { 
           'event-element': 'a',
           'event-description': '',
           'timestamp': 1506673474238,
         } 
    },
     { clickedEvents:
         { 
           'event-element': 'b',
           'event-description': '',
           'timestamp': 1506673474123,
         } 
    }]

Expected:

var a =    [ 
     { 
           'event-element': 'a',
           'event-description': '',
           'timestamp': 1506673474238,

     },
     { 
           'event-element': 'b',
           'event-description': '',
           'timestamp': 1506673474123,

    }]

Use Array.prototype.map

 var a = [ { clickedEvents: { 'event-element': 'a', 'event-description': '', 'timestamp': 1506673474238, } }, { clickedEvents: { 'event-element': 'b', 'event-description': '', 'timestamp': 1506673474123, } }]; a = a.map(function(o){ return o.clickedEvents; }); console.log(a);

a = a.map( el => el.clickedEvents );

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