How do you convert a multi-dimensional array into an array of objects using .reduce()?
Starting array
[
[
['juice', 'apple'], ['maker', 'motts'], ['price', 12]
],
[
['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]
]
]
And ending array
[
{juice: 'apple', maker: 'motts', price: 12},
{juice: 'orange', maker: 'sunkist', price: 11}
]
This is what I have now. This is really just me shooting in the dark.
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
}
You can try a combination of array.map
and array.reduce
.
Array.push
is suited to add element to an array, but if you want to transform all elements of an array to a certain specification, its always better to use 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)
You could use Array#map
in combination with Object.assign
for the properties with a spread syntax ...
.
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; }
Here is a solution using reduce
and foreach
.
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);
A solution using two 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);
You're actually close, except you seem to think array.push works like array.reduce - it doesn't
var transformData = array =>
array.map(subarray =>
subarray.reduce((result, [key, value]) => {
result[key] = value;
return result;
}, {})
);
Since you're using some ES2015+ features, I used more ... the destructuring of the inner array [key, value]
I just have to add this "extension" to the best answer so far
const transformData=_=>_.map(_=>Object.assign(..._.map(([$,_])=>({[$]:_}))));
Try reading that in a few months and know what it's doing :p
try this:
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)
Here's a succinct expression using map
and reduce
along with object spread syntax.
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)
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.