在javascript中从对象中删除空键值对

时间:2017-05-18 16:18:05

标签: javascript json object

如何从对象中删除空键值对 像

angular.module('myApp').controller('LoginController', LoginCtrl);

LoginCtrl.$inject = ['$q', '$scope', '$location', '$routeParams', '$window', 'authService', 'regService'];

function LoginCtrl($q, $scope, $location, $routeParams, $window, authService, regService) {
  //some code here
}

将成为obj = {name: 'asd','':'',pass: true}

提前致谢

3 个答案:

答案 0 :(得分:3)

试试这个delete obj[''];

有关delete运算符的更多信息: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/delete

答案 1 :(得分:1)

你可以试试:

delete obj[''];

答案 2 :(得分:1)

在一行中:

delete obj[''];

 for(property  in obj){ 
   if(property == ""){
   delete obj[property]
   }
 }