简体   繁体   English

如何在不使用循环的情况下将数组转换为 Javascript 中的对象?

[英]How to convert an array to an object in Javascript without using loops?

I want to convert the following array:我想转换以下数组:

['a', 'b', 'c']

to the following object:到以下对象:

{a: 'a', b: 'b', c: 'c'}

How do I do it without using loop, ofcourse?我如何在不使用循环的情况下做到这一点,当然?

You'll want to use the Array.reduce() method.您需要使用Array.reduce()方法。

The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value. reduce() 方法对数组的每个成员执行一个 reducer 函数(您提供的),从而产生一个输出值。

arr.reduce(callback[, initialValue])

The reduce() callback method takes accumulator and currentValue parameters. reduce()回调方法采用accumulatorcurrentValue参数。

  • accumulator: Value is remembered throughout each iteration of the method and ultimately becomes the final returned value.累加器:在方法的每次迭代中都会记住值,并最终成为最终的返回值。
  • currentValue: The current element being processed in the array . currentValue: array正在处理的当前元素。

The {} is supplied as the last argument to reduce() as the initial value to start with. {}作为reduce()的最后一个参数作为初始值提供。 And with each iteration of the Array , we add to it ultimately creating the final Object .并且随着Array每次迭代,我们添加到它最终创建最终的Object

Example: (ES6)示例: (ES6)

 const letters = ['a', 'b', 'c']; const obj = letters.reduce((accumulator, currentValue) => { accumulator[currentValue] = currentValue; return accumulator; }, {}); console.log(obj);

Example: (ES5)示例: (ES5)

 var letters = ['a', 'b', 'c']; var obj = letters.reduce(function (accumulator, currentValue) { accumulator[currentValue] = currentValue; return accumulator; }, {}); console.log(obj);

Reference: Array.prototype.reduce() mozilla documentation.参考: Array.prototype.reduce() mozilla 文档。

You could map objects and join to a single object with Object.assign .您可以使用Object.assign映射对象并连接到单个对象。

 var array = ['a', 'b', 'c'], object = Object.assign(...array.map(v => ({ [v]: v }))); console.log(object);

An alternative solution, you can also use the newer Object.fromEntries with map as well.另一种解决方案是,您也可以将较新的Object.fromEntries与 map 一起使用。

 let arr = ['a', 'b', 'c']; let obj = Object.fromEntries(arr.map(m => [m,m])); console.log(obj);

You may use Array#reduce to get the desired result.您可以使用Array#reduce来获得所需的结果。

 const a = ['a', 'b', 'c']; const o = a.reduce((s, a) => { s[a] = a; return s; }, {}); console.log(o);

Use reduce使用减少

 var arr = ['a', 'b', 'c']; var obj = arr.reduce(function(obj, value) { obj[value] = value; return obj }, {}); console.log(obj)

The object.assign() might solve your problem. object.assign()可能会解决您的问题。

Here is an example.这是一个例子。

var array = ['a', 'b', 'c']; 

Object.assign( {}, array);

// {0: "a", 1: "b", 2: "c"}

The new object will have same index with the array.新对象将与数组具有相同的索引。

If you want the values of the array to be same with the key.如果您希望数组的值与键相同。

function arrayToObject( array) 
{  
    var object = array.reduce( (obj, value) => {
        obj[value] = value;
        return obj
    }, {});
    return object;
}
arrayToObject( ['a', 'b', 'c'])

这可能是最短的版本:

 console.log( ['a', 'b', 'c'].reduce((o, v) => (o[v] = v, o), {}) )

const values = ['a', 'b', 'c'] const objectValues = {...values} => const values = ['a', 'b', 'c'] const objectValues = {...values} =>

 const values = ['a', 'b', 'c'] const objectValues = {...values} console.log(objectValues)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用Object.assign的情况下使用javascript将数组转换为Object - How to Convert an array into Object using javascript without using Object.assign 如何使用没有箭头功能的 javascript 将此 javascript object 转换为数组? - How do I convert this javascript object into an array using javascript without arrow functions? Javascript 使用 Lodash,将数组转换为普通 object,没有键 - Javascript using Lodash, convert array to plain object, without keys 如何在没有依赖属性的情况下将数组转换为JavaScript中的object - How to convert an array to object in JavaScript without dependency properties 如何获取数组A和数组B的所有项目并将其保存在数组C中? 使用JavaScript而不使用循环 - How to get all items that are both is array A and B and save them in array C? using javascript without using loops 如何在不使用for循环的情况下从JavaScript数组的每个数组中返回两个最大数? - how to return the two largest number from each array of an array in JavaScript without using for loops? 将数组转换为 object 而不对 javascript 中的键进行排序 - Convert array to object without sorting keys in javascript 如何解释使用 for 循环在 JavaScript 中反转数组 - How to explain reversing an array in JavaScript using for loops 如何在Javascript中将数组转换为对象? - How to convert array to object in Javascript? 如何在 JavaScript 中将对象转换为数组? - How to convert an object into an array in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM