Javascript ES6 - Extract Object Properties

// Function const extract = ({id, text, completed = false}) => ({id, text, completed}); // Test const todos = [ {id: 0, text: 'foo'}, {id: 1, text: 'bar', completed: true}, {id: 2, text: 'baz', version: 2} ]; extract(todos[0]); // » Object {id: 0, text: "foo", completed: false} extract(todos[1]); // » Object {id: 1, text: "bar", completed: true} extract(todos[2]); // » Object {id: 2, text: "baz", completed: false}
How to extract certain properties from an object, with defaults.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.