簡體   English   中英

將具有特定值對的對象數組轉換為單個對象

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM