如何在 JavaScript 中将对象的所有值设置为 null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45897879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:18:42  来源:igfitidea点击:

How to set all values of an object to null in JavaScript?

javascript

提问by Pilgrim

I need to set all properties of some object to null. But the object can be very big, so I can't just do it one by one.

我需要将某个对象的所有属性设置为null. 但是对象可能很大,所以我不能一个一个地做。

How to set all properties at once?

如何一次设置所有属性?

回答by Nianyi Wang

Here's a useful function called 'Object.keys()', it returns all of the attribute names of an object.

这是一个名为“Object.keys()”的有用函数,它返回对象的所有属性名称。

let setAll = (obj, val) => Object.keys(obj).forEach(k => obj[k] = val);
let setNull = obj => setAll(obj, null);

Non-arrow-function version:

非箭头功能版本:

function setAll(obj, val) {
    /* Duplicated with @Maksim Kalmykov
        for(index in obj) if(obj.hasOwnProperty(index))
            obj[index] = val;
    */
    Object.keys(obj).forEach(function(index) {
        obj[index] = val
    });
}
function setNull(obj) {
    setAll(obj, null);
}

回答by Maksim Kalmykov

You can use Object.keys()as Nianyi Wang mentioned in his answer, or a for in, like this:

您可以使用Object.keys()王念一在他的回答中提到的,或者for in像这样:

for (key in obj) {
    if (obj.hasOwnProperty(key)) {
        obj[key] = null;
    }
}

But in this case you should check hasOwnProperty().

但在这种情况下,您应该检查hasOwnProperty().

回答by Xun Yang

Another way of doing it, using Array.reduce. It does not overwriting the existing object. This only works if the object only have simple values.

另一种方法是使用Array.reduce. 它不会覆盖现有对象。这仅在对象只有简单值时才有效。

const newObj = Object.keys(originalObj).reduce(
  (accumulator, current) => {
    accumulator[current] = null; 
    return accumulator
  }, {});

回答by Xun Yang

But the object can be very big, so I can't just do it one by one.

但是对象可能很大,所以我不能一个一个地做。

By "big" do you mean "millions of properties" and you are concerned about performance? Or do you mean "a bunch of properties you don't know the names of, and/or don't want to have list out"?

“大”是指“数百万个属性”吗?您关心性能吗?或者您的意思是“一堆您不知道名称和/或不想列出的属性”?

How to set all properties at once?

如何一次设置所有属性?

You can't. One way or another, you have to loop.

你不能。无论如何,你必须循环。

Instead of mutating an existing object, consider creating a new, empty object. Its property values will be undefined, but that could work depending on your code structure.

与其改变现有对象,不如考虑创建一个新的空对象。它的属性值为undefined,但这可能取决于您的代码结构。

回答by Malaji Nagaraju

If object contains child object, if you want to set all child object properties to null, recursive solution is below.

如果对象包含子对象,如果要将所有子对象属性设置为空,递归解决方案如下。

function setEmpty(input){

let keys = Object.keys(input);

    for( key of keys ){

         if(typeof input[key] != "object" ){
             input[key] = null;
         }else{
             setEmpty(input[key]);
         }
    }
    return input;
}

回答by Bruno Souza

let values = {
    a:1, 
    b:'', 
    c: {
        a:'', 
        s:4, 
        d: {
            q: '',
            w: 8,
            e: 9
        }
 
    }
}


values;

const completeWithNull = (current) => {
  Object.keys(current).forEach((key) => {
     current[key] = current[key] === ''? null 
     : typeof current[key] === 'object' ? completeWithNull(current[key])
     : current[key]
  });
  
  return current;
};

completeWithNull(values);