Whenever you’re looking for reducing an array of values to one value, you’re looking for .reduce():
state = {
fields: fields.reduce((acc, key) => ({...acc, [key]: ''}), {}),
};
More Related Contents:
- Are ‘Arrow Functions’ and ‘Functions’ equivalent / interchangeable?
- What’s the meaning of “=>” (an arrow formed from equals & greater than) in JavaScript?
- What do multiple arrow functions mean in JavaScript?
- What is destructuring assignment and its uses?
- Arrow function without curly braces
- ES6 class variable alternatives
- Create object from class name in JavasScript ECMAScript 6
- What are the differences (if any) between ES6 arrow functions and functions bound with Function.prototype.bind?
- How do I destructure all properties into the current scope/closure in ES2015?
- How can I mock an ES6 module import using Jest?
- How to destructure object properties with key names that are invalid variable names?
- React Native: require() with Dynamic String?
- React functional stateless component, PureComponent, Component; what are the differences and when should we use what?
- Destructuring and rename property
- Unexpected behavior using Array Map on an Array Initialized with Array Fill
- Why “this” is undefined inside a fat arrow function definition? [duplicate]
- How do I persist a ES6 Map in localstorage (or elsewhere)?
- Active link with React-Router?
- Immediate function using JavaScript ES6 arrow functions
- Why couldn’t popular JavaScript runtimes handle synchronous-looking asynchronous script?
- Define a custom hash() method for use with ES6 maps
- javascript import from ‘/folder’ with index.js
- How to test if an object is a Proxy?
- How to import part of object in ES6 modules
- Web Workers – How To Import Modules
- Chrome console already declared variables throw undefined reference errors for let
- How to delete property from spread operator?
- How to run Node.js app with ES6 features enabled?
- ES2017 – Async vs. Yield
- Why does `obj.foo = function() { };` not assign the name `foo` to the function?