简体   繁体   中英

convert an array of objects to a single object with certain value pairs

Im aware there are many questions on how to turn an array into an object I know you can do something like this.. object = {...arr}; but my question is more specific

say I have an array of objects like so..

[
    {
       dataType: something,
       dataValue: book
    },
    {
       dataType: food,
       dataValue: brocolli
    },
    {
       dataType: object,
       dataValue: chair
    },
]

and I want to create an object that looks like this..

{
   something: book,
   food: brocolli,
   object: chair
}

I tried to do something like this...

arr.forEach(item => {
   newarray.push({arr.dataType: arr.dataValue});
})
newObject = {...newarray}

but that didnt work.. any help would be appreciated!

You can use 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); 

You can use the function 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; } 

Using Spread Syntax and computed property names.

 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; } 

Some Array.prototype.reduce() , destructuring and spread do the job:

 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;},{})

You probably want to use newarray[item.dataType] = item.dataValue; to set to set those key-value pairs into the 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); 

As an alternative to reduce , you can use Object.assign() and Array.map() . This is very similar to your original attempt, except for using object assign. I prefer this method, because the spread is only performed once, after the mapping has been created. My gut tells me this is more performant than reduce , but I haven't tested.

 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM