admin管理员组文章数量:1379790
I have the following chaining operator
const age = data.student?.age ? data.student.age: '';
this works fine in my local machine but seems to have problem in another machine. on further investigation i could understand that node js lower version(i suppose version below 12) doesn't support chaining operator. I understand that i can replace this with if condition like below but would like to know suggestions on what is the best possible alternative for this.
function checkAge(data){
if(data && data.student && data.student.age) {
return data.student.age;
} else {
return '';
}
}
const age = checkAge(data);
I have the following chaining operator
const age = data.student?.age ? data.student.age: '';
this works fine in my local machine but seems to have problem in another machine. on further investigation i could understand that node js lower version(i suppose version below 12) doesn't support chaining operator. I understand that i can replace this with if condition like below but would like to know suggestions on what is the best possible alternative for this.
function checkAge(data){
if(data && data.student && data.student.age) {
return data.student.age;
} else {
return '';
}
}
const age = checkAge(data);
Share
Improve this question
edited Jun 17, 2022 at 10:16
VLAZ
29.1k9 gold badges63 silver badges84 bronze badges
asked Jun 17, 2022 at 10:02
ShaSha
1,9946 gold badges35 silver badges60 bronze badges
5
-
It is strange that for something that would be a number (
age
) you want to return a string (''
) when the property does not exist. That seems like a bad choice. – trincot Commented Jun 17, 2022 at 10:07 -
Of course the best option is to upgrade node,
Node. js 12 will reach End-of-Life status on 30 April 2022
. Unsupported Node versions is of course a massive security issue. – Keith Commented Jun 17, 2022 at 10:09 - "best possible alternatives" is opinion based. – trincot Commented Jun 17, 2022 at 10:10
- How to avoid 'cannot read property of undefined' errors? – adiga Commented Jun 17, 2022 at 10:11
- @Sha Optional chaining alternative way found?? – Karuppiah RK Commented Dec 5, 2022 at 10:06
3 Answers
Reset to default 4There is no need for code change. You only need to modify the target
option in your TypeScript configuration and set it to anything ES2019 or below. Then you can use optional chaining in your TypeScript code and the piler will produce the equivalent code.
The TypeScript code
const foo = a?.b?.c;
bees
"use strict";
var _a;
const foo = (_a = a === null || a === void 0 ? void 0 : a.b) === null || _a === void 0 ? void 0 : _a.c;
when piled: Playground Link
If the problem is readability, you could probably try object destructing. So, your assigment would look something like this:
const {
student: {
age = ''
} = {}
} = data
Assuming declaring age as const, not to polluting scope with intermediate variables, and returning a number even for '0' and empty string in case of undefined are all a must, shorter option that es to my mind would be following:
const age = (d=>(isNaN(d=(d.student||{}).age)?'':d))(data);
For less strict approaches, cleaner solution would be:
const age = (data.student || {}).age || "";
// In this case, though, numeric 0 would also be returned as empty string.
On the other hand, if you need to do this more than a few times, I would remend to implement a handy picking function like:
const pick = (target, path)=>path.split(".")
.reduce(
(acc,key)=>acc&&acc[key]
,target
)
;
// And then...
const age = pick(data, 'student.age');
For picking approach it would be worth to reduce the number of function calls performed by pick() function. I just used reduce for the sake of brevity and simplicity.
本文标签:
版权声明:本文标题:node.js - Best alternative for optional chaining in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744429754a2605882.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论