简体   繁体   中英

Transform Array of Objects to spesific Object structure Javascript

 [ { "Hari Libur":{ "date":"12-07" } }, { "Hari Libur":{ "date":"12-12" } }, { "Hari Libur":{ "date":"12-04" } ]

Into this Object structure

 { 'Hari Libur': {date: '12/07'}, 'Hari Libur': {date: '12/12'}, 'Hari Libur': {date: '12/04'} }

I have tried with Object.assign but it also include me with 0:{}, 1:{}, 2:{}.

Is there any other way to do?

var arr =[
   {
      "Hari Libur":{
         "date":"12-07"
      }
   },
   {
      "Hari Libur1":{
         "date":"12-12"
      }
   },
];
var obj ={}
var res = arr.map(d=>{
  var key = Reflect.ownKeys(d);
  var date = d[key].date;
  obj[key[0]]={"date":date}
})
console.log(obj)

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