简体   繁体   English

javascript用从另一个对象数组的键值获得的键创建对象

[英]javascript create an object with key obtained from value of a key from another array of objects

I am creating an object from the array of objects, using the value for one of the keys: 我正在使用键之一的值从对象数组创建一个对象:

 const myArray = [ { uuid: '123', name: 'abc' }, { uuid: '789', name: 'xyz' } ]; const newObj = {}; for (var i = 0; i < myArray.length; i++) { newObj[myArray[i].uuid] = myArray[i]; } console.log('result: ', newObj) 

how can I do the same using ecma6 practices? 如何使用ecma6做法做同样的事情?

Using the functions reduce , Spread syntax , Computed property names and Arrow functions . 使用函数reduceSpread syntaxComputed property namesArrow functions

 const myArray = [ { uuid: '123', name: 'abc' }, { uuid: '789', name: 'xyz' } ]; var newObj = myArray.reduce((a, c) => ({...a, [c.uuid]: c}), {}); console.log('result: ', newObj) 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Resources 资源

You could use Object.assign() with map() and spread syntax ... 您可以将Object.assign()map()并扩展语法...

 const myArray = [{uuid: '123',name: 'abc'},{uuid: '789',name: 'xyz'}]; const newObj = Object.assign({}, ...myArray.map(e => ({[e.uuid]: e}))) console.log(newObj) 

The other answers are fancy, but I think the most readable, and simplest answer is the following: 其他答案都不错,但是我认为最容易理解和最简单的答案如下:

 const myArray = [{uuid: '123',name: 'abc'},{uuid: '789',name: 'xyz'}]; const newObj = {} myArray.forEach(item => newObj[item.uuid] = item) console.log(newObj) 

Yes this was available in version 5, but I think is applicable to the goal of the question. 是的,这在版本5中可用,但我认为适用于该问题的目标。

暂无
暂无

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

相关问题 用另一个数组/对象javascript的键值替换数组/对象的键值 - Replace array/objects key value with key value from another array/object javascript 将键/值数组中的对象与另一个对象(键/值)进行比较 - Comparing objects from key/value array with another object (Key/value) 我想创建一个 object,其中键作为 object 的属性,值作为与键匹配的对象数组(javascript) - I want to create an object with the key as a property from the object and value as an array of objects matching the key (javascript) Javascript从该数组中附加对象并创建附加了对象键值对的新数组 - Javascript Append objects from this array and create new array with object key value pairs appended 使用 JavaScript 将键值添加到另一个数组中的所有对象 - Add key value to all objects in array from another with JavaScript 从对象数组中创建一个 object 按 javascript 中名为“PageName”的键的值分组 - Create a object from array of objects by group by value of key called "PageName" in javascript 如何从数组字符串动态创建 JavaScript 中的对象键和值 - how to create Object key & value in JavaScript dynamically from array string 使用具有不同键值对的 typescript 从另一个对象数组创建对象数组 - create an array of objects from another array of objects using typescript with different key value pair 使用一个 object 的值作为新 object 的键,从一组对象创建一个 object - Create an object from an array of objects using the value from one object as the key for the new object 使用键和变量值创建javascript对象 - create javascript object with key and value from variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM