问题
I am having an array of objects and I want to create another object based on their keys. For example I am having an array as
var arr = [{1: 36011, 2: 18320, 3: 36011, 4: 10570},
{1: 19754, 2: 6722, 3: 19754, 4: 6699},
{1: 15711, 2: 10039, 3: 15711, 4: 4172}]
and I want my result array as
var result = {1:[36011,19754,15711], 2:[18320,6722,10039],..}
I was suggested to use lodash, I am new to this so I've tried using reduce
var i = 1, new_arr = {};
_.reduce(arr, function(key, val){
new_arr[i++] = temp1.key
return new_arr;
},{})
I am getting the values as undefined. What is the mistake, Can anyone help me with this?
回答1:
with lodash you can use _.mergeWith() and concat the values in the customiser function:
const arr = [{"1":36011,"2":18320,"3":36011,"4":10570},{"1":19754,"2":6722,"3":19754,"4":6699},{"1":15711,"2":10039,"3":15711,"4":4172}]
const result = _.mergeWith({}, ...arr, (ov = [], sv) => ov.concat(sv))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
回答2:
This might look intimidating at first. Start reducing the dataset from an empty object. Then reduce each row starting with result of outer reduce and build on the result so far.
const addToBucket = (bucket = {}, k, v, prev = bucket[k] || []) => ({
...bucket,
[k]: [...prev, v],
})
const toBucket = list =>
list.reduce(
(bucket, row) =>
Object.entries(row).reduce((b, [k, v]) => addToBucket(b, k, v), bucket),
{},
)
const data = [
{ 1: 36011, 2: 18320, 3: 36011, 4: 10570 },
{ 1: 19754, 2: 6722, 3: 19754, 4: 6699 },
{ 1: 15711, 2: 10039, 3: 15711, 4: 4172 },
]
console.log(toBucket(data))
// Nested list, as requested
console.log(Object.entries(toBucket(data)).map(([_, v]) => v))
回答3:
You can try like this in simplest way.
This is also available online at https://rextester.com/IPI35964 to try and see output.
var arr = [{1: 36011, 2: 18320, 3: 36011, 4: 10570},
{1: 19754, 2: 6722, 3: 19754, 4: 6699},
{1: 15711, 2: 10039, 3: 15711, 4: 4172}]
var output = {}
for(var obj of arr){
for(var key in obj){
if(key in output){
output[key].push(obj[key])
} else {
output [key] = [obj[key]]
}
}
}
console.log(output)
Output
{ '1': [ 36011, 19754, 15711 ],
'2': [ 18320, 6722, 10039 ],
'3': [ 36011, 19754, 15711 ],
'4': [ 10570, 6699, 4172 ] }
回答4:
One fairly clean way of doing this:
- Create one big array of key-value pairs
- Group those key-value pairs by their keys
- Extract the values from the grouped key-value pairs
var arr = [
{ 1: 36011, 2: 18320, 3: 36011, 4: 10570 },
{ 1: 19754, 2: 6722, 3: 19754, 4: 6699 },
{ 1: 15711, 2: 10039, 3: 15711, 4: 4172 }
];
// Get one big array of all key-value pairs
var pairs = _.flatMap(arr, _.entries);
// Group by the keys (index 0 of the pairs)
var groups = _.groupBy(pairs, 0);
// Extract the values
var final = _.mapValues(groups, _.partial(_.map, _, 1));
//console.log(pairs);
console.log(final);
// Using chaining:
var final2 = _.chain(arr)
.flatMap(_.entries)
.groupBy(0)
.mapValues(_.partial(_.map, _, 1));
console.log(final2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
来源:https://stackoverflow.com/questions/59122448/create-an-object-based-on-the-keys-in-array-of-objects