简体   繁体   English

JS或jQuery如何删除键值对

[英]JS or jQuery how to delete key-value pair

Using JS or jQuery how to delete the key-value pair which the value is of type “ Null ” & "" . 使用JS或jQuery如何删除值类型为“ Null ”和""的键-值对。 eg before: 例如之前:

Object {style: "fruit", origin: "Thailand", day: "18d", color: "", weight: null}

alter: 改变:

Object {style: "fruit", origin: "Thailand", day: "18d"}

There are two parts to this: 这有两个部分:

  1. Loop through the object's properties 遍历对象的属性

  2. Remove a property from an object 从对象中删除属性

There are lots of ways to do the first, covered by this question's answers . 这个问题的答案涵盖了很多方法。 Assuming you only care about "own" (non-inherited) properties, I'd probably use Object.keys to get an array of property names and then loop that. 假设您只在乎“自己的”(非继承)属性,我可能会使用Object.keys来获取属性名称数组,然后对其进行循环。

The second is done with the delete operator. 第二个是用delete运算符完成的。

So: 所以:

Object.keys(theObject).forEach(function(key) {
    var value = theObject[key];
    if (value === "" || value === null) {
        delete theObject[key];
    }
});

Live Example: 现场示例:

 var theObject = { style: "fruit", origin: "Thailand", day: "18d", color: "", weight: null }; console.log("Before:", JSON.stringify(theObject, null, 2)); Object.keys(theObject).forEach(function(key) { var value = theObject[key]; if (value === "" || value === null) { delete theObject[key]; } }); console.log("After:", JSON.stringify(theObject, null, 2)); 

You can use for..in to go through the loop to find which key has null or "" . 您可以使用for..in循环查找哪个键具有null""

Then use delete to delete the key 然后使用delete删除密钥

var myObj = {
  style: "fruit",
  origin: "Thailand",
  day: "18d",
  color: "",
  weight: null
}

for(var keys in myObj){
 if(myObj[keys] ===null || myObj[keys] === ""){
  delete myObj[keys]
 }
}
console.log(myObj)

JSFIDDLE 的jsfiddle

An easy solution by iterating threw the keys of your object and pushing properties matching in a resulting Array : 一个简单的解决方案,通过迭代抛出对象的键并在结果Array中推送匹配的属性:

var input = {style: "fruit", origin: "Thailand", day: "18d", color: "", weight: null};
var keys = Object.keys(input);
var result = {};
keys.forEach(key => {if (input[key] != null && input[key] != "") result[key] = input[key]});
console.log(result); // { style: 'fruit', origin: 'Thailand', day: '18d' }
var yourObj={style: "fruit", origin: "Thailand", day: "18d", color: "", weight: null}

for(var attr in yourObj){
  if(!yourObj[attr]){
    delete yourObj[attr]
  }
}

It can be done using this code : 可以使用以下代码完成:

var map = {style: "fruit", origin: "Thailand", day: "18d", color: "", weight: null};

for (var i in map){
    if(map[i]==null || map[i]==""){
       delete(map[i]);
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用CouchDB / Node.js中的Cradle删除键值对? - How do I delete a key-value pair using Cradle in CouchDB/Node.js? 在JQuery中处理键值对以进行图表制作 - Handling key-value pair in JQuery for charting 在js中某个对象的键值对中找到特定值 - find a specific value in key-value pair of some object in js 使用普通 JS 将表转换为键值对 - Convert table to key-value pair with plain JS Minimist js 库不返回键值对 - Minimist js library not returning key-value pair 查找具有特定键值对的JS对象/哈希 - Find JS object/hash with specific key-value pair 是否可以使用两个参数(map 的键值对,首选项)创建 function 并在首选项为“否”时删除键值对? - Is it possible to create a function taking two parameters (key-value pair of a map, preference) & delete the key-value pair if the preference is “no”? 将键值对jquery数组传递到.NET MVC操作 - Passing key-value pair jquery array into .NET MVC action 键值对Json转换并转换为Javascript / Jquery中的类 - Key-Value Pair Json transformation and conversion into a class in Javascript / Jquery 如何将两个数组制作成一个键值对字典以从 JS 传递给 HTML - How to make two arrays into a single key-value pair dictionary to pass to HTML from JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM