簡體   English   中英

如何使用.reduce()將數組轉換為對象數組

[英]How to convert array into array of objects using .reduce()

如何使用.reduce()將多維數組轉換為對象數組?

起始數組

[
    [
        ['juice', 'apple'], ['maker', 'motts'], ['price', 12]
    ],
    [
        ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]
    ]
]

和結束數組

[
    {juice: 'apple', maker: 'motts', price: 12},
    {juice: 'orange', maker: 'sunkist', price: 11}
]

這就是我現在所擁有的。 這真的只是我在黑暗中拍攝。

var transformData = (array) => {
    var newArray = array.push(function (all, item, index) {
        var newItem = item.reduce(function (all, item, index) {
            all[item[0]] = item[1]
            return all
        }, {})
        return all
    }, [])
    newArray.push(newItem)
    return newArray
}

您可以嘗試的組合array.maparray.reduce

Array.push適合將元素添加到數組中,但是如果要將數組的所有元素轉換為特定規范,最好使用array.map

 var data = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ] var result = data.map(function(list){ return list.reduce(function(o, kv){ o[kv[0]] = kv[1]; return o; }, {}); }) console.log(result) 

您可以將Array#mapObject.assign結合使用,以具有擴展語法...的屬性。

 var data = [[['juice', 'apple'], ['maker', 'motts'], ['price', 12]], [['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]]], result = data.map(o => Object.assign(...o.map(p => ({ [p[0]]: p[1] })))); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

這是使用reduceforeach的解決方案。

 var items = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ]; var transformData = items.reduce((newArr, currArr) => { var obj = {}; currArr.forEach((x) => { obj[x[0]] = x[1]; }); return newArr.concat(obj); },[]); console.log(transformData); 

一個解決方案使用兩個reduce。

 var items = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ]; var transformData = items.reduce((newArr, currArr) => { return newArr.concat(currArr.reduce((o, arr) =>{ o[arr[0]] = arr[1]; return o; }, {})); },[]); console.log(transformData); 

您實際上已經關閉了,只是您似乎認為array.push的工作方式類似於array.reduce-事實並非如此

var transformData = array => 
    array.map(subarray => 
        subarray.reduce((result, [key, value]) => {
            result[key] = value;
            return result;
        }, {})
    );

由於您使用的是ES2015 +的某些功能,因此我使用了更多的方法...內部數組[鍵,值]的結構分解

我只需要將此“擴展名”添加到到目前為止的最佳答案

const transformData=_=>_.map(_=>Object.assign(..._.map(([$,_])=>({[$]:_}))));

嘗試在幾個月內閱讀,然后知道它在做什么:p

嘗試這個:

var parray=[
[
    ['juice', 'apple'], ['maker', 'motts'], ['price', 12]
],
[
    ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]
]
];

var newArray=[]
parray.forEach(function(data){
var cak  =  data.reduce(function(a, b) {
a[b[0]]=b[1]
  return a;
}, {})
newArray.push(cak)
})
console.log(newArray)

這是一個使用mapreduce以及對象傳播語法的簡潔表達式。

 const data = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ] const data2 = data.map (pairs => pairs.reduce ((o, [k,v]) => ({ ...o, [k]: v }), {})) console.log (data2) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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