Convert array of object to single object in javascript(Angularjs)

后端 未结 3 1708
孤城傲影
孤城傲影 2021-01-24 05:46

How to do convert array of objects like this:

[
   { 
      display_name: \"view_dashboard\",
      value: 1
   },
   { 
      display_name: \"view_user\",
              


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-24 06:10

    You can try Array.map

    var data = [{
      display_name: "view_dashboard",
      value: 1
    }, {
      display_name: "view_user",
      value: 0
    }];
    
    var result = data.map(function(o){
      var _tmp = {};
      _tmp[o.display_name] = o.value
      return _tmp;
    });
    
    document.write("
    " + JSON.stringify(result,0,4) + "
    ");

提交回复
热议问题