null 在尾递归函数中被赋值为对象

编程入门 行业动态 更新时间:2024-10-09 17:19:28
本文介绍了null 在尾递归函数中被赋值为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个尾递归函数,其目的是在任意数量的嵌套 objects 中找到任意 number 并简单地运行 toFixed()就可以了.

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.

函数 formatData() 通过循环遍历 object 来工作,测试以查看当前迭代所在的值的类型,在所述迭代上执行函数 (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. The end result will be a an identical object but with any long decimal place numbers trimmed to two decimal places.

在测试过程中我发现,如果object 中任何级别的null 值,它都会首先保存为null但后来最终成为一个object.我知道在 JS 中 null 被认为是 object 类型,typeof 关键字将显示这一点.

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. I'm aware that in JS null is considered of type object and the typeof keyword will show this.

这是问题的代码笔:codepen.io/Danny_Wobble/pen/YzqMzxw请注意,第三个控制台条目显示正确分配/保存为 null 的值.稍后如所描述的那样,它最终成为一个 object.

Here is a codepen of the problem: codepen.io/Danny_Wobble/pen/YzqMzxw Please note that the third console entry shows the value being correctly assigned/saved as null. Later on as described it ends up as an object.

功能:

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 }

给定:

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)

预期(格式化数据):

{ "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" } }

推荐答案

您需要从对象检查中排除 null 值.

You need to exclude null values from the object check.

if (data[key] && typeof data[key] === 'object') // ^^^^^^^^^^^^

您可以更改仅检查不同处理的值的逻辑,例如数字和对象和 continue 或仅将给定值分配给新对象.

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.

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);

更多推荐

null 在尾递归函数中被赋值为对象

本文发布于:2023-11-29 18:48:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   值为   函数   对象   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!