Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert two arrays into an object in JavaScript?

I have two arrays, one for the players names :

players_name = ['John','Paul','Jack',...]

And another for the points of the players :

points = [23,13,18,...]

And I would like to know if it was possible to (not manually) transform this two arrays into an object that would look like this :

players_results = {
    'John': { point: 23},
    'Paul': { point: 13},
    'Jack': { point: 18},
}

Thanks !

like image 363
Mipix Avatar asked Jul 26 '21 12:07

Mipix


People also ask

Can we convert array to object in JavaScript?

To convert an array to an object, use the reduce() method to iterate over the array, passing it an object as the initial value. On each iteration, assign a new key-value pair to the accumulated object and return the result. Copied!

How can I merge two arrays in JavaScript?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.

How do you combine arrays?

In order to combine (concatenate) two arrays, we find its length stored in aLen and bLen respectively. Then, we create a new integer array result with length aLen + bLen . Now, in order to combine both, we copy each element in both arrays to result by using arraycopy() function.

Can we map two arrays in JavaScript?

To map multiple arrays with JavaScript, we can use the map method. to define the zip function that calls a1. map to combine the entries from a1 and a2 with index i into one entry. Then we call zip with arr1 and arr2 to zip them into one array.

How do I convert an array to an object in JavaScript?

We'll go over each of those methods in this article and provide some code examples for you to use on your website or application. Let's get started! 1. Object.assign () Object.assign () is the first method we'll cover for converting an array to an object. This method is used to copy values from one or more source objects to a new object.

How to write a JavaScript function that takes in an array?

We are required to write a JavaScript function that takes in one such array of arrays. Here, each subarray represents one key-value pair, the first element being the key and the second its value. The function should construct an object based on the key-value pairs in the array and return the object.

How to enumerate over each item in an array in JavaScript?

For the second method, we're going to loop over each item in an array and add each of its values as a new property in a new object. To match the object returned in the last section, the key value will be the array index for each item. In the code example, we'll be using a for loop to enumerate over each item in the array.

How to convert array to object in AutoCAD?

Table of Contents 1 Object.assign () Object.assign () is the first method we'll cover for converting an array to an object. ... 2 Loop Over Array & Construct a New Object For the second method, we're going to loop over each item in an array and add each of its values as ... 3 Reduce ()


4 Answers

You can use Object.fromEntries() for achieving your required result.

fromEntries takes an array of arrays (the nested array contains two values, 1st one is the key and the 2nd one is the value) and then converts it to an object. Try this-

const players_name = ['John','Paul','Jack'];
const points = [23,13,18];

const result = Object.fromEntries(
  players_name.map((name, i) => ([name, {point: points[i]}]))
);

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

Note: This is not a good idea to use a name as the key of an object. Names might be similar for different people. Use a unique value as a key instead.

like image 120
Sajeeb Ahamed Avatar answered Nov 09 '22 10:11

Sajeeb Ahamed


const players_name = ['John', 'Paul', 'Jack']
const points = [23, 13, 18]

const result = players_name.reduce((prev, item, i) => {
  prev[item] = {
    point: points[i]
  }
  return prev;
}, {});

console.log(result)
like image 29
Yuanzhe Cui Avatar answered Nov 09 '22 10:11

Yuanzhe Cui


const players_name = ['John','Paul','Jack'];
    
const points = [23,13,18];

const players_results = {}

players_name.forEach((data, index) => {
  players_results[data] = {
      point: points[index]
    }
});

console.log(players_results);
like image 21
Stan50 Avatar answered Nov 09 '22 10:11

Stan50


You could do with Array#forEach

const players_name = ['John','Paul','Jack']
const points = [23,13,18]

players_results ={};

players_name.forEach((a,i)=>{
  players_results[a] = {point:points[i]}
})

console.log(players_results)
like image 23
prasanth Avatar answered Nov 09 '22 11:11

prasanth