
- 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
Transform data from a nested array to an object in JavaScript
Suppose, we have the following array of arrays −
const arr = [ [ ['dog', 'Harry'], ['age', 2] ], [ ['dog', 'Roger'], ['age', 5] ] ];
We are required to write a JavaScript function that takes in one such nested array. The function should then prepare an object based on the array.
The object for the above array should look like −
const output = [ {dog: 'Harry', age: 2}, {dog: 'Roger', age: 5} ];
Example
The code for this will be −
const arr = [ [ ['dog', 'Harry'], ['age', 2] ], [ ['dog', 'Roger'], ['age', 5] ] ]; const prepareObjectArray = (arr = []) => { const copy = arr.slice(); copy.forEach((el, ind, array) => { el.forEach((element, index, subArray) => { subArray[element[0]] = element[1]; }); el.length = 0; array[ind] = Object.assign({}, array[ind]); }); return copy; }; console.log(prepareObjectArray(arr));
Output
And the output in the console will be −
[ { dog: 'Harry', age: 2 }, { dog: 'Roger', age: 5 } ]
- Related Articles
- Transform nested array into normal array with JavaScript?
- From JSON object to an array in JavaScript
- Extract key value from a nested object in JavaScript?
- How to transform a JavaScript iterator into an array?
- Building a frequency object from an array JavaScript
- Sum of nested object values in Array using JavaScript
- MongoDB query to find data from an array inside an object?
- How to transform object of objects to object of array of objects with JavaScript?
- JavaScript - summing numbers from strings nested in array
- Remove item from a nested array by indices in JavaScript
- JavaScript creating an array from JSON data?
- How to convert nested array pairs to objects in an array in JavaScript ?
- Constructing a nested JSON object in JavaScript
- Get value for key from nested JSON object in JavaScript
- Retrieve key and values from object in an array JavaScript

Advertisements