
- 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
JavaScript: create an array of JSON objects from linking two arrays
Suppose, we have two arrays like these −
const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ];
We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array.
Therefore, the output for the above arrays should look like −
const output = { "breakfast" : ["eggs", "yogurt", "toast"], "lunch": ["falafel", "mushrooms", "fries"], "dinner": ["pasta", "cheese"] };
Example
The code for this will be −
const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ]; const combineMealAndIngredient = (meals, ingredients) => { const res = {}; meals.forEach(function (el, ind) { this[el] = ingredients[ind]; }, res); return res; }; console.log(combineMealAndIngredient(meals, ingredients));
Output
And the output in the console will be −
{ breakfast: [ 'eggs', 'yogurt', 'toast' ], lunch: [ 'falafel', 'mushrooms', 'fries' ], dinner: [ 'pasta', 'cheese' ] }
- Related Articles
- How to combine two arrays into an array of objects in JavaScript?
- Create array from JSON object JavaScript
- Extract arrays separately from array of Objects in JavaScript
- How to create an array of partial objects from another array in JavaScript?
- How to Create an Array using Intersection of two Arrays in JavaScript?
- Concatenate two arrays of objects and remove repeated data from an attribute in JavaScript?
- Convert array of objects to an object of arrays in JavaScript
- Array of objects to array of arrays in JavaScript
- JavaScript creating an array from JSON data?
- How can we make an Array of Objects from n properties of n arrays in JavaScript?
- How to convert JSON string to array of JSON objects using JavaScript?
- From JSON object to an array in JavaScript
- JavaScript Converting array of objects into object of arrays
- JavaScript JSON Arrays
- Convert array of arrays to array of objects grouped together JavaScript

Advertisements