[英]null being assigned as object in tail recursive function
I have a tail recursive function whose purpose is to find any number
in any number of nested objects
and simply run the toFixed()
on it.我有一个尾递归函数,其目的是在任意数量的嵌套
objects
找到任意number
,然后简单地在其上运行toFixed()
。
The function formatData() works by looping through the object
, testing to see what type of value is the current iteration is on, performing a function(toFixed()) on said iteration and saving it in a new object or do nothing and just saving the value as is.函数 formatData() 通过循环遍历
object
,测试以查看当前迭代是什么类型的值,在所述迭代上执行函数(toFixed()) 并将其保存在一个新对象中,或者什么都不做只保存原样的价值。 The end result will be a an identical object but with any long decimal place numbers trimmed to two decimal places.最终结果将是一个相同的对象,但任何长的小数位数都被修剪为两位小数。
During testing I've found that if there is a null
value in the object
at any level, it is first saved as null
but later ends up being an object
.在测试过程中,我发现如果在任何级别的
object
中有一个null
值,它首先被保存为null
但后来最终成为一个object
。 I'm aware that in JS null
is considered of type object
and the typeof
keyword will show this.我知道在 JS 中
null
被认为是object
类型, typeof
关键字将显示这一点。
Here is a codepen of the problem: https://codepen.io/Danny_Wobble/pen/YzqMzxw Please note that the third console entry shows the value being correctly assigned/saved as null
.这是问题的代码笔: https ://codepen.io/Danny_Wobble/pen/YzqMzxw 请注意,第三个控制台条目显示正确分配/保存为
null
。 Later on as described it ends up as an object
.稍后如所描述的那样,它最终成为一个
object
。
Function:功能:
function formatData(data) {
const formattedData = {}
for (const key in data) {
if (data[key] === null) formattedData[key] = null
// if (data[key] === null) formattedData[key] = 'null' // have tried saving as diff val, didn't work
if (data[key] === false) formattedData[key] = false
if (typeof data[key] === 'number') formattedData[key] = data[key].toFixed(2)
if (typeof data[key] === 'object') formattedData[key] = formatData(data[key]) // tail recursion
}
return formattedData
}
Given:鉴于:
const data = {
status: false,
survey: {
2018: 3.4288,
2019: 3.47701,
2020: null,
},
benchmarks: {
company: 3.455,
industry: 3.5,
portfolio: 3.4,
},
}
const formattedData = formatData(data)
Expectation (formattedData):期望(格式化数据):
{
"status": false,
"survey": {
"2018": "3.43",
"2019": "3.48",
"2020": null,
},
"benchmarks": {
"company": "3.50",
"industry": "3.50",
"portfolio": "3.40",
},
}
Actual result (formattedData):实际结果(格式化数据):
{
"status": false,
"survey": {
"2018": "3.43",
"2019": "3.48",
"2020": {} // this should be `null`
},
"benchmarks": {
"company": "3.50",
"industry": "3.50",
"portfolio": "3.40"
}
}
You need to exclude null
values from the object check.您需要从对象检查中排除
null
值。
if (data[key] && typeof data[key] === 'object')
// ^^^^^^^^^^^^
You could change the logic for checking only the values for different processing, like numbers and objects and continue
or just assign the given value to the new object.您可以更改逻辑以仅检查不同处理的值,例如数字和对象,然后
continue
或仅将给定值分配给新对象。
function formatData(data) { const formattedData = {} for (const key in data) { if (typeof data[key] === 'number') { formattedData[key] = data[key].toFixed(2) continue; } if (data[key] && typeof data[key] === 'object') { formattedData[key] = formatData(data[key]); continue; } formattedData[key] = data[key]; } return formattedData; } const data = { status: false, survey: { 2018: 3.4288, 2019: 3.47701, 2020: null }, benchmarks: { company: 3.455, industry: 3.5, portfolio: 3.4 } }, formattedData = formatData(data); console.log(formattedData);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.