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
- For-each over an array in JavaScript
- How to make a discord bot create an invite for every server it joins?
- How do I check if an array includes a value in JavaScript?
- 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?
- How can a Discord bot create a hyperlink in a Discord message in an embed or in general? [closed]
- Find object by id in an array of JavaScript objects
- How to you store data for each discord user in the discord server?
- Babel 6 regeneratorRuntime is not defined
- TypeError: Converting circular structure to JSON in nodejs
- What does [object Object] mean?
- Babel 7 – ReferenceError: regeneratorRuntime is not defined
- How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)
- Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai
- Split string into array
- Client on Node.js: Uncaught ReferenceError: require is not defined
- Error: Can’t set headers after they are sent to the client
- How can I remove a specific item from an array?
- How can a Discord bot create a hyperlink in a Discord message in an embed or in general?
- Array of JSON Objects
- Array.size() vs Array.length
- Sort array of objects by string property value
- Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai
- require is not defined? Node.js
- Client on Node.js: Uncaught ReferenceError: require is not defined
- Error: Can’t set headers after they are sent to the client
- TypeError: Router.use() requires middleware function but got a Object
- Express error – TypeError: Router.use() requires middleware function but got a Object
- Copy array by value
- How can I reverse an array in JavaScript without using libraries?
- Cannot GET / Nodejs Error
- What is NODE_ENV and how to use it in Express?
- Node.js heap out of memory
- What is the way of declaring an array in JavaScript?
- Reactjs – Form input validation
- How to create an associative array in JavaScript literal notation
- await is only valid in async function
- How to set NODE_ENV to production/development in OS X
- await is only valid in async function
- Remove duplicate values from JS array [duplicate]
- How can I create a two dimensional array in JavaScript?
- How to loop through an array containing objects and access their properties
- Get the last item in an array
- How do I empty an array in JavaScript?
- Heroku: “No default language could be detected for this app” error thrown for node app
- Remove duplicate values from JS array
- How can I create a two dimensional array in JavaScript?
- Heroku: “No default language could be detected for this app” error thrown for node app
- TypeError: Router.use() requires middleware function but got a Object
- How to loop through an array containing objects and access their properties
- Get the last item in an array
- What does “res.render” do, and what does the html file look like?
- How to filter an array/object by checking multiple values
- How to obtain username from Promise
? - How do you check if a variable is an array in JavaScript?
- NPM ELIFECYCLE error – using node server.js command
- How do I empty an array in JavaScript?
- Node.js – SyntaxError: Unexpected token import
- ReferenceError: fetch is not defined
- ‘react-scripts’ is not recognized as an internal or external command
- After $npm install, Getting Error: Cannot find module ‘../lib/utils/unsupported.js’
- How to find the sum of an array of numbers
- Node.js – SyntaxError: Unexpected token import
- SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag
- Remove Object from Array using JavaScript
- Short circuit Array.forEach like calling break
- Invalid shorthand property initializer
- nodejs – error self signed certificate in certificate chain
- Remove duplicate values from JS array
- What is the most efficient way to deep clone an object in JavaScript?
- How to check if array is empty or does not exist?
- Why is using “for…in” for array iteration a bad idea?
- What’s the source of Error: getaddrinfo EAI_AGAIN?
- stop all instances of node.js server
- Deleting array elements in JavaScript – delete vs splice
- Using Node.js require vs. ES6 import/export
- Filter array to have unique values
- How to randomize (shuffle) a JavaScript array?
- How to compare arrays in JavaScript?
- ReferenceError : window is not defined at object.
Node.js - Why is process.env.NODE_ENV undefined?
- Node.js Port 3000 already in use but it actually isn’t?
- Better way to sum a property value in an array
- Unable to verify leaf signature
- npm – EPERM: operation not permitted on Windows
- How can I mock an ES6 module import using Jest?
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript