简体   繁体   English

设置多个 object 键的最简洁方法

[英]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.

相关问题 在 JavaScript 中编写排序方法的最简洁方法,该方法对多个 object 属性进行排序 - Cleanest way to write a sort method in JavaScript that sorts on multiple object properties 将此数组转换为单个对象的最简洁方法? - Cleanest way to convert this array into a single object? 在复杂例程中使用多个值的最简洁方法? - Cleanest way to use multiple values in a complex routine? 编写 if 与多个 else 条件的最简洁方法 - Cleanest way to write if with multiple else with condition 设置动态样式元素的最干净的方法是什么? - What's the cleanest way to set dynamic style elements? 将JavaScript对象转换为多维数组的最简洁方法 - Cleanest way to convert JavaScript object to multi-dimensional array 基于对象属性的Javascript最干净的方法来区分数组 - Javascript cleanest way to distinct array based on object property 将此 object 转换为密钥对对象数组的最简洁方法是什么? - What is the cleanest way to convert this object into an array of key-pair objects? 在JavaScript中修复此JSON对象字符串的最干净的方法是什么? - Whats the cleanest way to repair this JSON object string in javascript? 使用 ES6,在 object 中重新分配属性的最简洁方法是什么 - Using ES6, what is the cleanest way to reassign properties in an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM