Given two arrays the task is to create an object from them where the first array contains the keys of the object and the second array contains the values of the object. Return null if the array lengths are not the same or if the arrays are empty.
We can create an object from two arrays in Javascript in the following ways:
Example:
Input:
Array 1 => [1, 2, 3, 4]
Array 2 => ["ram", "shyam", "sita", "gita"]
Output:
{
1: "ram",
2: "shyam",
3: "sita",
4: "gita"
}
Method 1: Using for-each loop
The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array.
Example: In this example, we will be using Javascript`s for-each loop.
Javascript
let a = [1, 2, 3, 4];
let b = [ "ram" , "shyam" , "sita" , "gita" ]
function convertToObj(a, b) {
if (a.length != b.length ||
a.length == 0 ||
b.length == 0) {
return null ;
}
let obj = {};
a.forEach((k, i) =>
{ obj[k] = b[i] })
return obj;
}
console.log(convertToObj(a, b))
|
Output
{ '1': 'ram', '2': 'shyam', '3': 'sita', '4': 'gita' }
The Object.assign() method is used to copy the values and properties from one or more source objects to a target object.
Example: In this example, we will be using Javascript`s Object.assign method.
Javascript
let a = [1, 2, 3, 4];
let b = [ "ram" , "shyam" , "sita" , "gita" ]
function convertToObj(a, b) {
if (a.length != b.length ||
a.length == 0 ||
b.length == 0) {
return null ;
}
return Object.assign(...a.map((k, i) =>({
[k]: b[i] })))
}
console.log(convertToObj(a, b))
|
Output
{ '1': 'ram', '2': 'shyam', '3': 'sita', '4': 'gita' }
Method 3: Using reduce() method
The Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.
Example: In this example, we will be using Javascript`s reduce() method.
Javascript
let a = [1, 2, 3, 4];
let b = [ "ram" , "shyam" , "sita" , "gita" ];
function convertToObj(a, b) {
if (a.length != b.length ||
a.length == 0 ||
b.length == 0) {
return null ;
}
let object = a.reduce((acc, element, index) => {
return {
...acc,
[element]: b[index],
};
}, {});
return object;
}
console.log(convertToObj(a, b));
|
Output
{ '1': 'ram', '2': 'shyam', '3': 'sita', '4': 'gita' }
Method 4: Using Loop
This is the basic loop method for creating the object from the two arrays. We can iterate over one of the arrays and use the loop index to access corresponding elements from both arrays.
Example:
Javascript
function createObject(keys, values) {
let result = {};
let length = Math.min(keys.length, values.length);
for (let i = 0; i < length; i++) {
result[keys[i]] = values[i];
}
return result;
}
let a = [1, 2, 3, 4];
let b = [ "ram" , "shyam" , "sita" , "gita" ];
let result = createObject(a, b);
console.log(result);
|
Output
{ '1': 'ram', '2': 'shyam', '3': 'sita', '4': 'gita' }
The Object.fromEntries() method in JavaScript is a standard built-in object which is used to transform a list of key-value pairs into an object.
Example:
Javascript
function createObject(keys, values) {
const obj = Object.fromEntries(
keys.map((key, index) => [key, values[index]]),
);
return obj;
}
let a = [1, 2, 3, 4];
let b = [ "ram" , "shyam" , "sita" , "gita" ];
let result = createObject(a, b);
console.log(result);
|
Output
{ '1': 'ram', '2': 'shyam', '3': 'sita', '4': 'gita' }
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
10 Jul, 2023
Like Article
Save Article