- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Wrap object properties of type string with arrays - JavaScript
For this, use Object.keys() along with reduce(). To display the result, we will also use concat().
Example
Following is the code −
var details = { name: ["John", "David"], age1: "21", age2: "23" }, output = Object .keys(details) .reduce((obj, tempKey) => (obj[tempKey] = [].concat(details[tempKey]), obj), {}) console.log(output)
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo302.js.
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo302.js { name: [ 'John', 'David' ], age1: [ '21' ], age2: [ '23' ] }
Advertisements