[英]How to convert an array of nested objects to an object with key, value pairs in JavaScript
[英]convert an array of objects to a single object with certain value pairs
我知道有很多关于如何将数组转换为对象的问题,我知道您可以执行以下操作object = {...arr};
但我的问题更具体
说我有很多这样的对象..
[
{
dataType: something,
dataValue: book
},
{
dataType: food,
dataValue: brocolli
},
{
dataType: object,
dataValue: chair
},
]
我想创建一个看起来像这样的对象。
{
something: book,
food: brocolli,
object: chair
}
我试图做这样的事情...
arr.forEach(item => {
newarray.push({arr.dataType: arr.dataValue});
})
newObject = {...newarray}
但这没有用..任何帮助将不胜感激!
您可以使用reduce
var arr = [{ dataType: 'something', dataValue: 'book' }, { dataType: 'food', dataValue: 'brocolli' }, { dataType: 'object', dataValue: 'chair' }, ] var obj = arr.reduce((c, {dataType,dataValue}) => Object.assign(c, {[dataType]: dataValue}), {}); console.log(obj);
您可以使用函数reduce
。
var array = [ { dataType: 'something', dataValue: 'book' }, { dataType: 'food', dataValue: 'brocolli' }, { dataType: 'object', dataValue: 'chair' },]; var result = array.reduce((a, {dataType, dataValue}) => { a[dataType] = dataValue; return a; }, {}); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
使用扩展语法和计算的属性名称。
var array = [ { dataType: 'something', dataValue: 'book' }, { dataType: 'food', dataValue: 'brocolli' }, { dataType: 'object', dataValue: 'chair' },]; var result = array.reduce((a, {dataType, dataValue}) => ({...a, [dataType]: dataValue}), {}); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
一些Array.prototype.reduce()
, Array.prototype.reduce()
和传播可以完成这项工作:
var a = [ { dataType: 'something', dataValue: 'book' }, { dataType: 'food', dataValue: 'brocolli' }, { dataType: 'object', dataValue: 'chair' }, ]; // ES6 var new6 = a.reduce((o, {dataType, dataValue}) => ({...o, [dataType]: dataValue}), {}); console.log(new6); //ES5 var new5 = a.reduce(function(o, i) { var prop = {}; prop[i.dataType] = i.dataValue; return Object.assign(o, prop); }, {}); console.log(new5); //ES3.1 (lol) var new31 = {}, i; for (i = 0; i < a.length; i++) { new31[a[i].dataType] = a[i].dataValue; } console.log(new31);
像这个?
arr.reduce((acc,val)=>{acc[val.dataType]=val.dataValue;return acc;},{})
您可能要使用newarray[item.dataType] = item.dataValue;
设置以将这些键值对设置为newarray。
var arr = [ { dataType: 'something', dataValue: 'book' }, { dataType: 'food', dataValue: 'brocolli' }, { dataType: 'object', dataValue: 'chair' }, ]; var newarray = {}; arr.forEach(item => { newarray[item.dataType] = item.dataValue; }); newObject = {...newarray}; console.log(newObject);
作为reduce
的替代方法,可以使用Object.assign()
和Array.map()
。 除了使用对象分配外,这与您的原始尝试非常相似。 我更喜欢这种方法,因为在创建映射之后,扩展仅执行一次。 我的直觉告诉我,这比reduce
更具性能,但是我还没有测试。
var arr = [{ dataType: 'something', dataValue: 'book' }, { dataType: 'food', dataValue: 'brocolli' }, { dataType: 'object', dataValue: 'chair' }, ] var obj = Object.assign({}, ...arr.map(({ dataType, dataValue }) => ({ [dataType]: dataValue }))) console.log(obj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.