
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to turn a JSON object into a JavaScript array in JavaScript ?
Suppose, we have this JSON object where index keys are mapped to some literals −
const obj = { "0": "Rakesh", "1": "Dinesh", "2": "Mohit", "3": "Rajan", "4": "Ashish" };
We are required to write a JavaScript function that takes in one such object and uses the object values to construct an array of literals.
Example
The code for this will be −
const obj = { "0": "Rakesh", "1": "Dinesh", "2": "Mohit", "3": "Rajan", "4": "Ashish" }; const objectToArray = (obj) => { const res = []; const keys = Object.keys(obj); keys.forEach(el => { res[+el] = obj[el]; }); return res; }; console.log(objectToArray(obj));
Output
And the output in the console will be −
[ 'Rakesh', 'Dinesh', 'Mohit', 'Rajan', 'Ashish' ]
- Related Articles
- How to deserialize a JSON into Javascript object?
- How to transform JSON text into a JavaScript object?
- How to convert a JSON string into a JavaScript object?
- Converting two arrays into a JSON object in JavaScript
- Convert JSON array into normal json in JavaScript
- How to convert a date object's content into json in JavaScript?
- How to modify an array value of a JSON object in JavaScript?
- How to turn JavaScript array into the comma-separated list?
- How to turn a String into a JavaScript function call?
- From JSON object to an array in JavaScript
- Create array from JSON object JavaScript
- Sorting a JSON object in JavaScript
- Flattening a JSON object in JavaScript
- How do I turn a string in dot notation into a nested object with a value – JavaScript?
- Strip quotes with JavaScript to convert into JSON object?

Advertisements