
- 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 can we make an Array of Objects from n properties of n arrays in JavaScript?
Suppose we have two arrays of literals like these −
const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false];
We are required to write a JavaScript function that creates and returns a new Array of Objects from these two arrays, like this −
const response = [ {opt: 'A', val: true}, {opt: 'B', val: false}, {opt: 'C', val: false}, {opt: 'D', val: false}, ];
Example
Following is the code −
const options = ['A', 'B', 'C', 'D']; const values = [true, false, false, false]; const mapArrays = (options, values) => { const res = []; for(let i = 0; i < options.length; i++){ res.push({ opt: options[i], val: values[i] }); }; return res; }; console.log(mapArrays(options, values));
Output
This will produce the following output on console −
[ { opt: 'A', val: true }, { opt: 'B', val: false }, { opt: 'C', val: false }, { opt: 'D', val: false } ]
- Related Articles
- How to access properties of an array of objects in JavaScript?
- JavaScript: create an array of JSON objects from linking two arrays
- How do we loop through array of arrays containing objects in JavaScript?
- Extract arrays separately from array of Objects in JavaScript
- Split Array of items into N Arrays in JavaScript
- Sort an array of objects by multiple properties in JavaScript
- Convert array of objects to an object of arrays in JavaScript
- Array of objects to array of arrays in JavaScript
- How to combine two arrays into an array of objects in JavaScript?
- Can we search an array of objects in MongoDB?
- How can we serialize an array of objects using flexjson in Java?
- Can we share a method between JavaScript objects in an array?
- Sort Array of objects by two properties in JavaScript
- How can I merge properties of two JavaScript objects dynamically?
- Search from an array of objects via array of string to get array of objects in JavaScript

Advertisements