Here is how I did it:
var props = { id: 1, name: 'test', children: [] } //copy props but leave children out var newProps = { ...props } delete newProps.children console.log(newProps) // { id: 1, name: 'test' }
Is there a cleaner, simpler way?
You can use the destructuring assignment :
var props = { id: 1, name: 'test', children: [] } var {children:_, ...newProps} = props; console.log(newProps) // { id: 1, name: 'test' } console.log(_) // [] - as an "empty" placeholder
(with the same suggestion of properties for adding / distributing for ES7 that you already used)
var props = { id: 1, name: 'test', children: [] } function clone(orig, blacklistedProps) { var newProps = {}; Object.keys(props).forEach(function(key) { if (!blacklistedProps || blacklistedProps.indexOf(key) == -1) { newProps[key] = props[key]; } }); return newProps; } var newProps = clone(props, ['children']); console.log(newProps) // { id: 1, name: 'test' } var newProps1 = clone(props); console.log(newProps1) // { id: 1, name: 'test', children:[] }
Source: https://habr.com/ru/post/1241760/More articles:iOS - Adding a 2D image to a scenekit game - iosExecuting bash commands from a CMake file - bashHow to add SKSpriteNode to a scene in Swift? - swiftDjango datetime field - convert to timezone - pythonCMake does not redirect stderr with execute_process - stdoutAll elements are changed on click, not just on the target - javascriptProblem with Jenkins amazon-ecr - pluginsAngularJS: element follows cursor - javascriptDoes jQuery tooltip not align horizontally? - javascriptHide points in ChartJS LineGraph - javascriptAll Articles