
- 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 convert array into array of objects using map() and reduce() in JavaScript
Suppose we have an array of arrays like this −
const arr = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ];
We are required to write a JavaScript function that takes in one such array and returns a new array of objects built based on the input array.
So, for the above array, the output should look like this −
const output = [ {juice: 'apple', maker: 'motts', price: 12}, {juice: 'orange', maker: 'sunkist', price: 11} ];
Example
The code for this will be −
const arr = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ]; const arrayToObject = arr => { let res = []; res = arr.map(list => { return list.reduce((acc, val) => { acc[val[0]] = val[1]; return acc; }, {}); }); return res; }; console.log(arrayToObject(arr));
Output
The output in the console −
[ { juice: 'apple', maker: 'motts', price: 12 }, { juice: 'orange', maker: 'sunkist', price: 11 } ][ { juice: 'apple', maker: 'motts', price: 12 }, { juice: 'orange', maker: 'sunkist', price: 11 } ]
- Related Articles
- Convert 2D array to object using map or reduce in JavaScript
- Convert an array of objects into plain object in JavaScript
- Map multiple properties in array of objects to the same array JavaScript
- How to convert JSON string to array of JSON objects using JavaScript?
- How to convert Map keys to an array in JavaScript?
- Convert object of objects to array in JavaScript
- Convert object to array of objects in JavaScript
- Convert array into array of subarrays - JavaScript
- Convert 2d tabular data entries into an array of objects in JavaScript
- JavaScript: How to map array values without using "map" method?
- How to convert nested array pairs to objects in an array in JavaScript ?
- Convert array of arrays to array of objects grouped together JavaScript
- How to convert an array into a complex array JavaScript?
- How to convert an array into JavaScript string?
- How to convert a plain object into ES6 Map using JavaScript?

Advertisements