JavaScript / Vue -如何清空整个对象属性值



我有这个对象:

mappingData: {
id_feed : "some data...",
mapping_name: "some data...",
xml_file_url: "some data...",
encoding: "some data...",
import_period: "some data...",
token: "some data...",
user_id: "some data...",
projectFieldOptions: [
{ some object data },
{ some object dat }
],
safety: {
action_negative_diff: "some data...",
action_positive_diff: "some data...",
action_diff: "some data...",
auto_update_title: "some data...",
auto_update_description: "some data...",
auto_update_images: "some data...",
import_product_variations: "some data...",
},
},

如何清空整个对象属性价值吗?

我正在尝试的是

for (const prop of Object.getOwnPropertyNames(this.mappingData)) {
delete this.mappingData[prop];
}

为了回答这个问题,我对这个对象进行了一些修改。因为这不是真正的javascript对象。假设这是javascript对象

let mappingData = {
id_feed: "some data...",
mapping_name: "some data...",
xml_file_url: "some data...",
encoding: "some data...",
import_period: "some data...",
token: "some data...",
user_id: "some data...",
projectFieldOptions: [{ jj: "vfgfgfg" }, { kk: "vfgfgf" }],
safety: {
action_negative_diff: "some data...",
action_positive_diff: "some data...",
action_diff: "some data...",
auto_update_title: "some data...",
auto_update_description: "some data...",
auto_update_images: "some data...",
import_product_variations: "some data...",
},
};

结果如下,

{
"id_feed": "",
"mapping_name": "",
"xml_file_url": "",
"encoding": "",
"import_period": "",
"token": "",
"user_id": "",
"projectFieldOptions": "",
"safety": ""
}

答案是

let mappingData = {
id_feed: "some data...",
mapping_name: "some data...",
xml_file_url: "some data...",
encoding: "some data...",
import_period: "some data...",
token: "some data...",
user_id: "some data...",
projectFieldOptions: [{ jj: "vfgfgfg" }, { kk: "vfgfgf" }],
safety: {
action_negative_diff: "some data...",
action_positive_diff: "some data...",
action_diff: "some data...",
auto_update_title: "some data...",
auto_update_description: "some data...",
auto_update_images: "some data...",
import_product_variations: "some data...",
},
};



for (const prop of Object.getOwnPropertyNames(mappingData)) {
mappingData[prop] = "";
}
console.log(mappingData);




可以通过创建递归函数来清空嵌套对象和数组。

输入对象:

let mappingData = {
id_feed: "some data...",
mapping_name: "some data...",
xml_file_url: "some data...",
encoding: "some data...",
import_period: "some data...",
token: "some data...",
user_id: "some data...",
projectFieldOptions: [{jj: "vfgfgfg"}, {kk: "vfgfgf"}],
safety: {
action_negative_diff: "some data...",
action_positive_diff: "some data...",
action_diff: "some data...",
auto_update_title: "some data...",
auto_update_description: "some data...",
auto_update_images: "some data...",
import_product_variations: "some data...",
},
};

这个函数将返回空对象

function emptyObjectValue(obj) {
for (const prop of Object.getOwnPropertyNames(obj)) {
if (obj[prop] instanceof Array) {
obj[prop].forEach(o => {
emptyObjectValue(o);
})
} else if (obj[prop] instanceof Object) {
emptyObjectValue(obj[prop]);
} else {
console.log(obj[prop]);
obj[prop] = null //or make it empty string like "";
}
}
return obj;
}
console.log(emptyObjectValue(mappingData));

输出如下所示:

{
id_feed: null,
mapping_name: null,
xml_file_url: null,
encoding: null,
import_period: null,
token: null,
user_id: null,
projectFieldOptions: [ { jj: null }, { kk: null } ],
safety: {
action_negative_diff: null,
action_positive_diff: null,
action_diff: null,
auto_update_title: null,
auto_update_description: null,
auto_update_images: null,
import_product_variations: null
}
}

相关内容

  • 没有找到相关文章

最新更新