js去除物件屬性的空值
阿新 • • 發佈:2021-01-16
技術標籤:處理資料javascriptvue.jshtmlhtml5es6
js去除物件屬性的空值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
var obj = { name: "凡夫俗子", sex: "man", age: null, height: "" };
for(var key in obj) {
if(obj[key] === "" || obj[key] === null) {
delete obj[key]
}
}
console.log (obj)
</script>
</body>
</html>