如何从多深度 JavaScript 对象中删除空属性?

发布于 2024-10-18 13:25:24 字数 431 浏览 2 评论 0原文

我有这个 JS 对象:

var test = {"code_operateur":[""],"cp_cult":["",""],"annee":["2011"],"ca_cult":[""]}

当我使用这个函数时:

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

我得到:

{"cp_cult":["",""],"annee":["2011"]}

好吧,不错,但我想删除空的“cp_cult”属性(它是一个数组,而不是像另一个一样的字符串)。

注意:我不想手动删除密钥!

I've got this JS Object:

var test = {"code_operateur":[""],"cp_cult":["",""],"annee":["2011"],"ca_cult":[""]}

When I use this function:

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

I get:

{"cp_cult":["",""],"annee":["2011"]}

Okay not bad, but I'd like to remove the empty "cp_cult" property (which is an array and not a string like the other).

Note: I don't want to manually delete the key!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2024-10-25 13:25:24

看起来你在这里问了 2 个问题。

  1. 如何删除对象的属性;以及
  2. 如何判断一个对象是否实际上是一个数组。

您可以使用delete 运算符删除对象的属性。

delete test.cp_cult;

在 JavaScript 中,数组是对象,这意味着 typeof([]) 无济于事地返回 object。通常,人们通过使用框架中的函数(dojo.isArray 或类似的东西)或滚动自己的方法来确定对象是否为数组来解决此问题。

没有 100% 保证的方法可以确定对象是否实际上是数组。大多数人只是检查它是否具有数组的一些方法/属性 lengthpushpopshift< /code>、unshift 等。

It looks like you're asking 2 questions here.

  1. How do I remove a property of an object; and
  2. How can I tell if an object is actually an array.

You can delete a property of an object using the delete operator.

delete test.cp_cult;

In JavaScript arrays are objects, which means that typeof([]) unhelpfully returns object. Typically people work around this by using a function in a framework (dojo.isArray or something similar) or rolling their own method that determines if an object is an array.

There is no 100% guaranteed way to determine if an object is actually an array. Most people just check to see if it has some of the methods/properties of an array length, push, pop, shift, unshift, etc.

三人与歌 2024-10-25 13:25:24

尝试:

function isEmpty(thingy) {
 for(var k in thingy){
  if(thingy[k]) {
   return false;
  }
 }
 return true;
}

for(i in test) {
 if ( test[i] == "" || test[i] === null || (typeof test[i] == "object" && isEmpty(test[i])) ) {
  delete test[i];
 }
}

但是,根据对象的复杂性,您需要更高级的算法。例如,如果数组可以包含另一个空字符串数组(甚至更多级别)并且应该将其删除,那么您也需要检查这一点。

编辑:尝试制作一些东西来满足您的需求,请查看:http:// jsfiddle.net/jVHNe/

Try:

function isEmpty(thingy) {
 for(var k in thingy){
  if(thingy[k]) {
   return false;
  }
 }
 return true;
}

for(i in test) {
 if ( test[i] == "" || test[i] === null || (typeof test[i] == "object" && isEmpty(test[i])) ) {
  delete test[i];
 }
}

However, depending on the complexity of the object, you'd need more advanced algorithms. For example, if the array can contain another array of empty strings (or even more levels) and it should be deleted, you'd need to check for that as well.

EDIT: Trying to make something to fit your needs, please have a look at: http://jsfiddle.net/jVHNe/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文