
- 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 merge an array with an object where values are arrays - JavaScript
Suppose, we have an array and an object like these −
const arr = [1, 2, 3, 4, 5]; const obj = { group1: ["Ram", "Mohan", "Shyam"], group2: ["Jai", "Dinesh"], };
We are required to zip the array and the object so that the values in the array are assigned to the new objects that are keyed with the values in the object.
Like this −
const output = { group1: { "Ram": 1, "Mohan": 2, "Shyam": 3 }, group2: { "Jai": 4, "Dinesh": 5 } };
We will iterate over each array item and simultaneously assign values to the new object’s keys.
Example
Following is the code −
const arr = [1, 2, 3, 4, 5]; const obj = { group1: ["Ram", "Mohan", "Shyam"], group2: ["Jai", "Dinesh"], }; const zipObject = (arr, obj) => { const res = {}; for(let i = 0; i < arr.length; i++){ if(obj['group1'][i]){ if(!res['group1']){ res['group1'] = {}; }; res['group1'][obj['group1'][i]] = arr[i]; }else{ if(!res['group2']){ res['group2'] = {}; } res['group2'][obj['group2'][i - obj['group1'].length]] = arr[i]; }; }; return res; }; console.log(zipObject(arr, obj));
Output
This will produce the following output in console −
{ group1: { Ram: 1, Mohan: 2, Shyam: 3 }, group2: { Jai: 4, Dinesh: 5 } }
- Related Articles
- Merge two arrays with alternating Values in JavaScript
- How to return object from an array with highest key values along with name - JavaScript?
- Converting a JavaScript object to an array of values - JavaScript
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- Looping numbers with object values and push output to an array - JavaScript?
- Converting array of arrays into an object in JavaScript
- How to edit values of an object inside an array in a class - JavaScript?
- Convert array of objects to an object of arrays in JavaScript
- How to merge objects into a single object array with JavaScript?
- How to represent the source code of an object with JavaScript Arrays?
- How to import an Object with Sub Objects and Arrays in JavaScript?
- How to merge specific elements inside an array together - JavaScript
- JavaScript: replacing object keys with an array
- How to convert an object into an array in JavaScript?
- Using merge sort to recursive sort an array JavaScript

Advertisements