_的不确定行为

发布于 2025-01-17 16:24:12 字数 360 浏览 3 评论 0 原文

let a = {
   b  : null
}

_.get(a, 'b',''); //return null

但是

_.get (a, null, '') //return ''

null是_.get()的虚假值吗?

let a = {
   b  : {c:{
}
} //basically a nested object

我正在尝试做_。get(a,'bc','')。toString(),我会遇到错误,因为_.get return return null。写这篇文章的最佳可读方法是什么?

let a = {
   b  : null
}

_.get(a, 'b',''); //return null

but

_.get (a, null, '') //return ''

Is null a falsy value for _.get()?

let a = {
   b  : {c:{
}
} //basically a nested object

I'm trying to do _.get(a, 'b.c','').toString() and I'm getting error because _.get return null. What is the best readable way to write this?

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

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

发布评论

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

评论(2

绿光 2025-01-24 16:24:12

第三个参数 defaultValue 将仅用于未定义已解决的值,而不是 null 请参阅DOCS

如果您想要 null 未定义的,使用

(_.get(a, "b.c") ?? "").toString()

以下是使用不同机制定义默认值的不同结果:

let a;

a = { b: null };
_.get(a, "b", ""); // null
_.get(a, "b") ?? ""; // ""
_.get(a, "b") || ""; // ""

a = { };
_.get(a, "b", ""); // ""

a = { b: undefined };
_.get(a, "b", ""); // ""

a = { b: 0 };
_.get(a, "b") || ""; // ""
_.get(a, "b") ?? ""; // 0

The third argument defaultValue will only be returned for undefined resolved values, not null. see docs

If you want a fallback value for null or undefined, use the nullish coalescing operator:

(_.get(a, "b.c") ?? "").toString()

Below are different results using different mechanisms in defining default value:

let a;

a = { b: null };
_.get(a, "b", ""); // null
_.get(a, "b") ?? ""; // ""
_.get(a, "b") || ""; // ""

a = { };
_.get(a, "b", ""); // ""

a = { b: undefined };
_.get(a, "b", ""); // ""

a = { b: 0 };
_.get(a, "b") || ""; // ""
_.get(a, "b") ?? ""; // 0
往日情怀 2025-01-24 16:24:12

_.get(a, 'b','');

您请求属性 a['b'] 时,它恰好是 null,它不是 undefined,因此您得到 < code>null 作为最终结果。接下来,

_.get(a, null, '');

您请求属性a['null'],该属性不存在,因此它未定义,最终得到后备值 ''。最后,

let a = {b: {c: {}}};
_.get(a, 'b.c', '');

您似乎要求 a['b']['c'],这将是一个空对象。然而,点线路径表示法是 Lodash 特有的。如果您使用下划线,则默认情况下该路径会被解释为 a['bc'],该路径不存在且未定义。在这种情况下,您应该得到 '' 作为最终结果。即使您使用的是 a = {b: null} 的原始值,我认为该表达式不可能导致 null

事实上,如果您使用 Underscore 并且想要检索 b 属性中嵌套的 c 对象,则有两种选择。首先,您可以使用数组路径表示法,它可以在 Underscore 和 Lodash 之间移植:

_.get(a, ['b', 'c'], '');

其次,您可以重写 _.toPath 为所有 Underscore 函数启用点分路径表示法:

const originalToPath = _.toPath;
_.toPath = function(path) {
    if (_.isString(path)) return path.split('.');
    return originalToPath(path);
}

_.get(a, 'b.c', '');

我不推荐这样做,因为它可能会破坏尝试检索名称中包含句点字符 . 的属性的代码。我提到它是为了完整性;使用你自己的判断。

In

_.get(a, 'b','');

you ask for the property a['b'], which happens to be null, which isn't undefined, so you get null as the final result. Next, in

_.get(a, null, '');

you ask for the property a['null'], which doesn't exist, so it's undefined, and you end up with the fallback value ''. Finally, with

let a = {b: {c: {}}};
_.get(a, 'b.c', '');

you seem to be asking for a['b']['c'], which would be an empty object. However, the dotted path notation is Lodash-specific. If you are using Underscore, the path is interpreted as a['b.c'] by default, which doesn't exist and is undefined. In that case, you should get '' as the final result. I see no way this expression could result in null, even if you are using the original value of a = {b: null}.

If you are, in fact, using Underscore and you want to retrieve the nested c object within the b property, you have two options. Firstly, you can use array path notation, which is portable between Underscore and Lodash:

_.get(a, ['b', 'c'], '');

Secondly, you can override _.toPath to enable dotted path notation for all Underscore functions:

const originalToPath = _.toPath;
_.toPath = function(path) {
    if (_.isString(path)) return path.split('.');
    return originalToPath(path);
}

_.get(a, 'b.c', '');

I wouldn't recommend this, because it can break code that is trying to retrieve properties that contain the period character . in the name. I mention it for completeness; use your own judgment.

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