将数组转换为JavaScript中的对象
Let’s say you have the following array,
假设您有以下数组,
const nums = [1, 2, 3, 4, 5];
console.log(nums);
Output
输出量
(5) [1, 2, 3, 4, 5]
We know that nums is an array and we can see in the output that we get an array. Let’s convert it into an object,
我们知道nums是一个数组,我们可以在输出中看到得到一个数组。 让我们将其转换为对象
console.log(Object.assign({
}, nums));
Output
输出量
{0: 1, 1: 2, 2: 3, 3: 4, 4: 5}
Now we get an object displayed on the console!
现在,我们在控制台上显示了一个对象!
Let’s see how the Object.assign() method works? The Object.assign() method is built on top of the object class and it takes in two parameters; the target and the source respectively. It copies all values from the source to the target. Let’s see some more examples,
让我们看看Object.assign()方法如何工作? Object.assign()方法建立在对象类的顶部,它具有两个参数: 目标和源。 它将所有值从源复制到目标。 让我们再看一些例子
const arr=[
{
name:'harry', age:14},
{
name:'sam', age:40},
{
name:'gloria', age:16},
{
name:'riky', age:33},
]
console.log(arr);
Output
输出量
(4) [{…}, {…}, {…}, {…}]
0: {name: "harry", age: 14}
1: {name: "sam", age: 40}
2: {name: "gloria", age: 16}
3: {name: "riky", age: 33}
length: 4
__proto__: Array(0)
We have an arr array that contains objects as individual elements. In essence, we have an array of objects. We’ll use the object.assign() method to convert this array of objects into an object of objects.
我们有一个arr数组,其中包含对象作为单个元素。 本质上,我们有一个对象数组。 我们将使用object.assign()方法将该对象数组转换为对象对象。
const namesObj = Object.assign({
}, arr);
console.log(namesObj);
Output
输出量
{0: {…}, 1: {…}, 2: {…}, 3: {…}}
0: {name: "harry", age: 14}
1: {name: "sam", age: 40}
2: {name: "gloria", age: 16}
3: {name: "riky", age: 33}
__proto__: Object
Now we get back an object of objects! It’s pretty simple to use Object.assign() for these types of conversions but it’s more important to understand what’s going on and how under the hood. Can you think of the internal difference between an array and an object? Now, can you figure out how to write a function that takes in an object and converts it to an array?
现在我们取回对象的对象! 对于这些类型的转换,使用Object.assign()非常简单,但更重要的是要了解发生了什么以及如何进行内幕。 您能想到数组和对象之间的内部差异吗? 现在,您能找出如何编写一个将一个对象接收并转换为数组的函数的方法吗?
Let’s see how we’ll implement our function that is capable of converting an array to an object. We need two things- a key and a value. Let’s assume that we will send across the key as a parameter to our function which we will use as the key for our new object.
让我们看看如何实现将数组转换为对象的函数 。 我们需要两件事-密钥和值。 假设我们将键作为参数传递给函数,并将其用作新对象的键。
const convertArrtoObj = (arr, key) => {
};
Now we’ll create an empty object which we’ll fill with elements from our array. We’ll use the reduce() method to traverse through the array and put the current value against a key.
现在,我们将创建一个空对象,该对象将填充数组中的元素。 我们将使用reduce()方法遍历数组并将当前值放在键上。
const convertArrtoObj=(arr,key)=>{
const initObj={
};
return arr.reduce((obj,currVal)=>{
return{
...obj,
[currVal[key]]:currVal
}
},initObj);
};
const newnamesObj=convertArrtoObj(arr,'age');
console.log(newnamesObj);
Output
输出量
{14: {…}, 16: {…}, 33: {…}, 40: {…}}
14: {name: "harry", age: 14}
16: {name: "gloria", age: 16}
33: {name: "riky", age: 33}
40: {name: "sam", age: 40}
__proto__: Object
We converted our original names array to an object using the age of each person as the key.
我们使用每个人的年龄作为键将原始名称数组转换为对象。
翻译自: https://www.includehelp.com/code-snippets/convert-an-array-to-an-object-in-javascript.aspx
原文地址: https://blog.csdn.net/cumt951045/article/details/107799125
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
相关文章