[英]Cleanest way to set multiple object keys
Hi I was working on a text parser and I was wondering if there was a clean to rewrite the following code.嗨,我正在研究一个文本解析器,我想知道是否有一个干净的代码可以重写以下代码。 Concerning the way that an object's properties are set.
关于对象属性的设置方式。
//In the real case the parser returns varying result based on the param const parserResult = {'value':2,'syntaxError':false,'NaNError':false} const {value,syntaxError,NaNError} = parserResult const param = {someProperty:'any value'} //can have any properties //the problem under here param['value'] = value param['syntaxError'] = syntaxError param['NaNError'] = NaNError console.log(param)
Setting three properties like that one after an other is not all that eloquent does anyone now a cleaner solution?一个接一个地设置三个这样的属性并不是eloquent现在有人更清洁的解决方案吗? Thanks in advance.
提前致谢。 (complete code to test under here)
(在这里测试的完整代码)
const parseParam = param => { //In the real case the parser returns varying result based on the param const parserResult = {'value':2,'syntaxError':false,'NaNError':false} const {value,syntaxError,NaNError} = parserResult param['value'] = value param['syntaxError'] = syntaxError param['NaNError'] = NaNError return param } const parameters = [{someProperty:'test'},{someProperty:'someValue'}] const parsedParameters = parameters.map(parseParam) console.log(parsedParameters)
You should use spread operator for this kind of stuff.你应该对这种东西使用扩展运算符。
It will look like this:它看起来像这样:
const param = {...parserResult, someProperty:'any value'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.