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 !
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!
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.
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.
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.
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.
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.
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.
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 ()
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.
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)
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);
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With