
- 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
Add all records from one array to each record from a different array in JavaScript
Suppose we have two arrays of strings that contain data about some users like this −
const users = ['Rahul', 'Dinesh', 'Rohit']; const data = ["SOP1", "SOP2","SOP3","SOP4"];
We are required to write a JavaScript function that takes in two such arrays and returns a new array of objects.
The new array should contain an object for each possible combination of the user and data array.
Therefore, for the above array, the output should look like −
const output = [ { User: 'Rahul', SOP: 'SOP1' }, { User: 'Rahul', SOP: 'SOP2' }, { User: 'Rahul', SOP: 'SOP3' }, { User: 'Rahul', SOP: 'SOP4' }, { User: 'Dinesh', SOP: 'SOP1' }, { User: 'Dinesh', SOP: 'SOP2' }, { User: 'Dinesh', SOP: 'SOP3' }, { User: 'Dinesh', SOP: 'SOP4' }, { User: 'Rohit', SOP: 'SOP1' }, { User: 'Rohit', SOP: 'SOP2' }, { User: 'Rohit', SOP: 'SOP3' }, { User: 'Rohit', SOP: 'SOP4' } ];
Example
The code for this will be −
const users = ['Rahul', 'Dinesh', 'Rohit']; const data = ["SOP1", "SOP2","SOP3","SOP4"]; const multiplyUserData = (users = [], data = []) => { const res = []; users.forEach(user => { data.forEach(el => { res.push({ 'user': user, 'sop': el }); }); }); return res; }; console.log(multiplyUserData(users, data));
Output
And the output in the console will be −
[ { user: 'Rahul', sop: 'SOP1' }, { user: 'Rahul', sop: 'SOP2' }, { user: 'Rahul', sop: 'SOP3' }, { user: 'Rahul', sop: 'SOP4' }, { user: 'Dinesh', sop: 'SOP1' }, { user: 'Dinesh', sop: 'SOP2' }, { user: 'Dinesh', sop: 'SOP3' }, { user: 'Dinesh', sop: 'SOP4' }, { user: 'Rohit', sop: 'SOP1' }, { user: 'Rohit', sop: 'SOP2' }, { user: 'Rohit', sop: 'SOP3' }, { user: 'Rohit', sop: 'SOP4' } ]
- Related Articles
- Remove/ filter duplicate records from array - JavaScript?
- MongoDB Convert One record with an array to multiple records in a new collection?
- Create a record array from a (flat) list of array in Numpy
- Create a record array from a (flat) list of array and set a valid datatype for all in Numpy
- Create a record array from binary data in Numpy
- Get unique item from two different array in JavaScript
- How to filter an array from all elements of another array – JavaScript?
- Removing all the empty indices from array in JavaScript
- Finding all possible combinations from an array in JavaScript
- Deleting specific record from an array nested within another array in MongoDB
- Delete specific record from an array nested within another array in MongoDB?
- JavaScript recursive loop to sum all integers from nested array?
- Fetch records from a subdocument array wherein id begins from 234 in MongoDB
- Finding all the longest strings from an array in JavaScript
- Construct a record array from a wide-variety of objects in Numpy

Advertisements