ECMAScript 6 introduces the easily polyfillable Object.assign
:
The
Object.assign()
method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
Object.assign({}, ['a','b','c']); // {0:"a", 1:"b", 2:"c"}
The own length
property of the array is not copied because it isn’t enumerable.
Also, you can use ES8 spread syntax on objects to achieve the same result:
{ ...['a', 'b', 'c'] }
For custom keys you can use reduce:
['a', 'b', 'c'].reduce((a, v) => ({ ...a, [v]: v}), {}) // { a: "a", b: "b", c: "c" }
Related Posts:
- What’s the difference between “{}” and “[]” while declaring a JavaScript array?
- How can I access and process nested objects, arrays or JSON?
- What is the difference between ( for… in ) and ( for… of ) statements?
- Most efficient way to convert an HTMLCollection to an Array
- Sorting arrays in javascript by object key value
- push object into array
- How to add an object to an array
- How to remove all duplicates from an array of objects?
- Sort array of objects by single key with date value
- Most efficient method to groupby on an array of objects
- Complex JSON nesting of objects and arrays
- How to make a discord bot create an invite for every server it joins?
- map function for objects (instead of arrays)
- Start script missing error when running npm start
- Loop through an array in JavaScript
- How can I remove a specific item from an array?
- Find object by id in an array of JavaScript objects
- How to you store data for each discord user in the discord server?
- What does [object Object] mean?
- Split string into array
- Error: Can’t set headers after they are sent to the client
- Array.size() vs Array.length
- Client on Node.js: Uncaught ReferenceError: require is not defined
- TypeError: Router.use() requires middleware function but got a Object
- Cannot GET / Nodejs Error
- How to create an associative array in JavaScript literal notation
- How to set NODE_ENV to production/development in OS X
- How can I create a two dimensional array in JavaScript?
- Get the last item in an array
- How do I empty an array in JavaScript?
- How can I create a two dimensional array in JavaScript?
- Get the last item in an array
- How to obtain username from Promise
? - How do you check if a variable is an array in JavaScript?
- Node.js – SyntaxError: Unexpected token import
- ‘react-scripts’ is not recognized as an internal or external command
- After $npm install, Getting Error: Cannot find module ‘../lib/utils/unsupported.js’
- Short circuit Array.forEach like calling break
- What is the most efficient way to deep clone an object in JavaScript?
- Why is using “for…in” for array iteration a bad idea?
- Deleting array elements in JavaScript – delete vs splice
- How to compare arrays in JavaScript?
- ReferenceError : window is not defined at object.
Node.js - Node.js Port 3000 already in use but it actually isn’t?
- Unable to verify leaf signature
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript
- Fastest way to duplicate an array in JavaScript – slice vs. ‘for’ loop
- Merge/flatten an array of arrays
- AJAX response error: net::ERR_EMPTY_RESPONSE
- Express Render not working return error: No default engine was specified and no extension was provided
- Remove Object from Array using JavaScript
- How to use npm jquery module?
- Node.js Error: connect ECONNREFUSED
- TypeError: app.use() requires middleware functions
- No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’
- Node.js document is not defined
- How to handle ETIMEDOUT error?
- XMLHttpRequest module not defined/found
- Remove duplicate values from JS array
- Why is my asynchronous function returning Promise {
} instead of a value? - NodeJs : TypeError: require(…) is not a function
- What is the difference between node.js and io.js?
- module.exports vs exports in Node.js
- Basic Ajax send/receive with node.js
- Converting an object to a string
- How do I check if an object has a key in JavaScript?
- Remove last item from array
- Objects are not valid as a React child. If you meant to render a collection of children, use an array instead
- How to fix Cannot find module ‘typescript’ in Angular 4?
- Error: ENOENT: no such file or directory, stat ‘/public/main.html’ at Error (native)
- Simple average function in Javascript
- npm WARN enoent ENOENT: no such file or directory, open ‘C:\Users\Nuwanst\package.json’
- How to access the request body when POSTing using Node.js and Express?
- How to change value of object which is inside an array using JavaScript or jQuery?
- SyntaxError: expected expression, got ‘<'
- use Lodash to sort array of object by value
- Eslint: How to disable “unexpected console statement” in Node.js?
- how to use react require syntax?
- Finding the average of an array using JS
- Is there a simple way to make a random selection from an array in JavaScript or any other programming language?
- Sorting object property by values
- Convert array to JSON
- NPM install fails with node-gyp
- How to fix Error: listen EADDRINUSE while using NodeJS?
- element.setAttribute is not a function
- JavaScript array to CSV
- console.log showing contents of array object
- console.log showing contents of array object
- How to sort 2 dimensional array by column value?
- How to convert binary string to decimal?
- How to disable auto-start of “Node.js: Server-side JavaScript” when using Adobe Dreamweaver?
- Write / add data in JSON file using Node.js
- Create an empty object in JavaScript with {} or new Object()?
- Make Discord bot send picture with message with NodeJS
- javascript push multidimensional array
- Find a value in an array of objects in Javascript [duplicate]
- Checking if a key exists in a JavaScript object?
- What does `node –harmony` do?
- Add JavaScript object to JavaScript object
- how to add inside a text box?