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? 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)
CodePudding user response:
You should use spread operator for this kind of stuff.
It will look like this:
const param = {...parserResult, someProperty:'any value'}