gpt4 book ai didi

javascript - 仅在定义值时才内联设置 JS 对象的属性

转载 作者:行者123 更新时间:2023-11-30 14:45:27 25 4
gpt4 key购买 nike

情况

我目前正在编写用于将数据推送到数据库的构造函数。

其中一个构造函数如下所示:

/**
* Timestamp
*
* @constructor
*/
function Timestamp() {
this.timestamp = new Date().getTime();
this.date = new Date().toUTCString();
}

/**
* @param {string} [parameters.status]
* @param {Timestamp} [parameters.sent]
* @param {Timestamp} [parameters.delivered]
* @constructor
*/
function Shipping(parameters) {
const { status, sent, delivered } = parameters;
this.status = status;
this.timestamps = {
sent: sent,
delivered: delivered
};
}

console.log(new Shipping({
status: "pending",
delivered: new Timestamp()
}));

后端更新设置的所有值(我使用的是 Firebase 数据库)。

在这个例子中有2个时间戳

  1. sent - 表示 cargo 已发送的时间戳
  2. delivered - 表示 cargo 到达的时间戳

在上面的场景中, cargo 已经在一段时间前发出。更新数据库的功能已被调用,因为 cargo 已到达。我们想更新delivered时间戳,sent时间戳。

另一方面,Firebase 对覆盖与更新相关的数据非常敏感。如果我们要使用上面的代码,我们会将 sent 时间戳设置为 undefined,这将从数据库中删除时间戳。所以我们根本不能为 sent 设置值。


问题

我可以在构造函数中写出函数来测试 undefined。那行得通,但它不漂亮且不易维护。尤其是考虑到庞大的数据库结构时(上面的例子没有显示不是整个extend)。

该代码可能看起来类似于(此代码有效):

/**
* Timestamp
*
* @constructor
*/
function Timestamp() {
this.timestamp = new Date().getTime();
this.date = new Date().toUTCString();
}

/**
* @param {string} [parameters.status]
* @param {Timestamp} [parameters.sent]
* @param {Timestamp} [parameters.delivered]
* @constructor
*/
function Shipping(parameters) {
const { status, sent, delivered } = parameters;
if (status !== undefined) this.status = status;
this.timestamps = {};
if (sent !== undefined) this.timestamps.sent = sent;
if (delivered !== undefined) this.timestamps.delivered = delivered;
}

console.log(new Shipping({
status: "pending",
delivered: new Timestamp()
}));


我的解决思路

我能想到的解决此问题的最佳“伪”方法是使用 OR 运算符进行内联测试。请注意,这不会起作用,因为至少根据我的经验,nullundefinedoverwrite firebase数据。

相反,我们“甚至不想碰”数据。

这是解决方案如果 null不覆盖 firebase 中的数据:

/**
* Timestamp
*
* @constructor
*/
function Timestamp() {
this.timestamp = new Date().getTime();
this.date = new Date().toUTCString();
}

/**
* @param {string} [parameters.status]
* @param {Timestamp} [parameters.sent]
* @param {Timestamp} [parameters.delivered]
* @constructor
*/
function Shipping(parameters) {
const { status, sent, delivered } = parameters;
this.status = status || null;
this.timestamps = {
sent: sent || null,
delivered: delivered || null
};
}

console.log(new Shipping({
status: "pending",
delivered: new Timestamp()
}));


如果 JS 对象包含一个值,我如何才能设置它的值?


解决方案

谢谢Jonas W.为您的解决方案!

工作代码:

/**
* Timestamp
*
* @constructor
*/
function Timestamp() {
this.timestamp = new Date().getTime();
this.date = new Date().toUTCString();
}

/**
* Remove all nested keys with value "undefined" in an object
* @param object
*/
function clean(object) {
Object.keys(object).forEach(key => {
if (object[key] && typeof object[key] === "object") {
// Iterate through nested keys
clean(object[key]);
} else if (object[key] === undefined) {
// Found a value which is undefined so we'll remove its key
delete object[key];
}
});
}

/**
* @param {string} [parameters.status]
* @param {Timestamp} [parameters.sent]
* @param {Timestamp} [parameters.delivered]
* @constructor
*/
function Shipping(parameters) {
const { status, sent, delivered } = parameters;
this.status = status;
this.timestamps = {
sent: sent,
delivered: delivered
};
clean(this);
}

console.log(new Shipping({
status: "pending",
delivered: new Timestamp()
}));

工作原理

在完成构造函数之前,我们执行 clean() 函数,该函数删除所有值为 undefined 的键。您还可以修改 clean() 函数以针对其他值进行测试。例如,您可以使用此函数删除所有值为 "remove me" 的键:

/**
* Remove all nested keys with value "remove me" in an object
* @param object
*/
function clean(object) {
Object.keys(object).forEach(key => {
if (object[key] && typeof object[key] === "object") {
// Iterate through nested keys
clean(object[key]);
} else if (object[key] === "remove me") {
// Found a value which is equal to "remove me" so we'll remove its key
delete object[key];
}
});
}

最佳答案

您可以清除对象中的未定义:

for(const key in obj)
if(obj[key] === undefined)
delete obj[key];

这可以在构造函数中完成,也可以在发送到数据库时完成。

关于javascript - 仅在定义值时才内联设置 JS 对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48996164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com