Reduce Multiple Objects into One
Let's say I have an array of objects:
[
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
However, I can't seem to get an array that looks like this:
{
category: 'Category ALL',
max: 16,
min: 17
}
All my attempts to make through reduce did not work.
javascript reduce
add a comment |
Let's say I have an array of objects:
[
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
However, I can't seem to get an array that looks like this:
{
category: 'Category ALL',
max: 16,
min: 17
}
All my attempts to make through reduce did not work.
javascript reduce
2
"... with success.", you say.
– ibrahim mahrir
Nov 12 '18 at 14:34
Is the issue in the fact you do not understand reduce, or are you getting errors?
– Dellirium
Nov 12 '18 at 14:35
Is there any algorithm behind that reduction? Or are the numbers and the category label drawn by random?
– Nico Haase
Nov 12 '18 at 14:36
@NicoHaase pretty sure its a simple sum
– Dellirium
Nov 12 '18 at 14:37
add a comment |
Let's say I have an array of objects:
[
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
However, I can't seem to get an array that looks like this:
{
category: 'Category ALL',
max: 16,
min: 17
}
All my attempts to make through reduce did not work.
javascript reduce
Let's say I have an array of objects:
[
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
However, I can't seem to get an array that looks like this:
{
category: 'Category ALL',
max: 16,
min: 17
}
All my attempts to make through reduce did not work.
javascript reduce
javascript reduce
edited Nov 13 '18 at 1:18
Paulpro
112k15224230
112k15224230
asked Nov 12 '18 at 14:32
LuisDemn
22
22
2
"... with success.", you say.
– ibrahim mahrir
Nov 12 '18 at 14:34
Is the issue in the fact you do not understand reduce, or are you getting errors?
– Dellirium
Nov 12 '18 at 14:35
Is there any algorithm behind that reduction? Or are the numbers and the category label drawn by random?
– Nico Haase
Nov 12 '18 at 14:36
@NicoHaase pretty sure its a simple sum
– Dellirium
Nov 12 '18 at 14:37
add a comment |
2
"... with success.", you say.
– ibrahim mahrir
Nov 12 '18 at 14:34
Is the issue in the fact you do not understand reduce, or are you getting errors?
– Dellirium
Nov 12 '18 at 14:35
Is there any algorithm behind that reduction? Or are the numbers and the category label drawn by random?
– Nico Haase
Nov 12 '18 at 14:36
@NicoHaase pretty sure its a simple sum
– Dellirium
Nov 12 '18 at 14:37
2
2
"... with success.", you say.
– ibrahim mahrir
Nov 12 '18 at 14:34
"... with success.", you say.
– ibrahim mahrir
Nov 12 '18 at 14:34
Is the issue in the fact you do not understand reduce, or are you getting errors?
– Dellirium
Nov 12 '18 at 14:35
Is the issue in the fact you do not understand reduce, or are you getting errors?
– Dellirium
Nov 12 '18 at 14:35
Is there any algorithm behind that reduction? Or are the numbers and the category label drawn by random?
– Nico Haase
Nov 12 '18 at 14:36
Is there any algorithm behind that reduction? Or are the numbers and the category label drawn by random?
– Nico Haase
Nov 12 '18 at 14:36
@NicoHaase pretty sure its a simple sum
– Dellirium
Nov 12 '18 at 14:37
@NicoHaase pretty sure its a simple sum
– Dellirium
Nov 12 '18 at 14:37
add a comment |
4 Answers
4
active
oldest
votes
You could reduce the data by taking min and max as sum in an object for the next iteration.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => ({ min: a.min + b.min, max: a.max + b.max }))
);
console.log(result);
A more dynamic approach by using an array for the keys.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
keys = ['min', 'max'],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => Object.assign(...keys.map(k => ({ [k]: a[k] + b[k] }))))
);
console.log(result);
add a comment |
var array = [{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = array.reduce((a, v) => {
a.max += v.max
a.min += v.min
return a
}, {
category: "Category All",
max: 0,
min: 0
})
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
Just pass in the initial object { category: 'Category ALL', max: 0, min: 0 }
to reduce
, and for each item, just add its max
and min
to that object:
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
Example:
let array = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }];
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
console.log(result);
add a comment |
You could use Array.reduce, so the array of objects can be reduced into a single object, and using es6 you can write in a single line.
Please see the below code,i hope it solves the issue
var res = [
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = res.reduce((a,c) => ({category:"Category All", max: a.max + c.max, min: a.min + c.min}))
console.log("result", result)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264342%2freduce-multiple-objects-into-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could reduce the data by taking min and max as sum in an object for the next iteration.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => ({ min: a.min + b.min, max: a.max + b.max }))
);
console.log(result);
A more dynamic approach by using an array for the keys.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
keys = ['min', 'max'],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => Object.assign(...keys.map(k => ({ [k]: a[k] + b[k] }))))
);
console.log(result);
add a comment |
You could reduce the data by taking min and max as sum in an object for the next iteration.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => ({ min: a.min + b.min, max: a.max + b.max }))
);
console.log(result);
A more dynamic approach by using an array for the keys.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
keys = ['min', 'max'],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => Object.assign(...keys.map(k => ({ [k]: a[k] + b[k] }))))
);
console.log(result);
add a comment |
You could reduce the data by taking min and max as sum in an object for the next iteration.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => ({ min: a.min + b.min, max: a.max + b.max }))
);
console.log(result);
A more dynamic approach by using an array for the keys.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
keys = ['min', 'max'],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => Object.assign(...keys.map(k => ({ [k]: a[k] + b[k] }))))
);
console.log(result);
You could reduce the data by taking min and max as sum in an object for the next iteration.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => ({ min: a.min + b.min, max: a.max + b.max }))
);
console.log(result);
A more dynamic approach by using an array for the keys.
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
keys = ['min', 'max'],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => Object.assign(...keys.map(k => ({ [k]: a[k] + b[k] }))))
);
console.log(result);
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => ({ min: a.min + b.min, max: a.max + b.max }))
);
console.log(result);
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => ({ min: a.min + b.min, max: a.max + b.max }))
);
console.log(result);
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
keys = ['min', 'max'],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => Object.assign(...keys.map(k => ({ [k]: a[k] + b[k] }))))
);
console.log(result);
var data = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }],
keys = ['min', 'max'],
result = Object.assign(
{ category: 'Category All' },
data.reduce((a, b) => Object.assign(...keys.map(k => ({ [k]: a[k] + b[k] }))))
);
console.log(result);
answered Nov 12 '18 at 14:37
Nina Scholz
176k1390155
176k1390155
add a comment |
add a comment |
var array = [{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = array.reduce((a, v) => {
a.max += v.max
a.min += v.min
return a
}, {
category: "Category All",
max: 0,
min: 0
})
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
var array = [{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = array.reduce((a, v) => {
a.max += v.max
a.min += v.min
return a
}, {
category: "Category All",
max: 0,
min: 0
})
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
var array = [{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = array.reduce((a, v) => {
a.max += v.max
a.min += v.min
return a
}, {
category: "Category All",
max: 0,
min: 0
})
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
var array = [{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = array.reduce((a, v) => {
a.max += v.max
a.min += v.min
return a
}, {
category: "Category All",
max: 0,
min: 0
})
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
var array = [{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = array.reduce((a, v) => {
a.max += v.max
a.min += v.min
return a
}, {
category: "Category All",
max: 0,
min: 0
})
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
var array = [{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = array.reduce((a, v) => {
a.max += v.max
a.min += v.min
return a
}, {
category: "Category All",
max: 0,
min: 0
})
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Nov 12 '18 at 14:38
George
4,45711731
4,45711731
add a comment |
add a comment |
Just pass in the initial object { category: 'Category ALL', max: 0, min: 0 }
to reduce
, and for each item, just add its max
and min
to that object:
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
Example:
let array = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }];
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
console.log(result);
add a comment |
Just pass in the initial object { category: 'Category ALL', max: 0, min: 0 }
to reduce
, and for each item, just add its max
and min
to that object:
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
Example:
let array = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }];
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
console.log(result);
add a comment |
Just pass in the initial object { category: 'Category ALL', max: 0, min: 0 }
to reduce
, and for each item, just add its max
and min
to that object:
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
Example:
let array = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }];
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
console.log(result);
Just pass in the initial object { category: 'Category ALL', max: 0, min: 0 }
to reduce
, and for each item, just add its max
and min
to that object:
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
Example:
let array = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }];
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
console.log(result);
let array = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }];
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
console.log(result);
let array = [{ category: 'Category A', max: 10, min: 12 }, { category: 'Category B', max: 2, min: 1 }, { category: 'Category C', max: 4, min: 4 }];
let result = array.reduce((acc, obj) => {
acc.max += obj.max;
acc.min += obj.min;
return acc;
}, { category: 'Category ALL', max: 0, min: 0 });
console.log(result);
answered Nov 12 '18 at 14:38
ibrahim mahrir
21.8k41847
21.8k41847
add a comment |
add a comment |
You could use Array.reduce, so the array of objects can be reduced into a single object, and using es6 you can write in a single line.
Please see the below code,i hope it solves the issue
var res = [
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = res.reduce((a,c) => ({category:"Category All", max: a.max + c.max, min: a.min + c.min}))
console.log("result", result)
add a comment |
You could use Array.reduce, so the array of objects can be reduced into a single object, and using es6 you can write in a single line.
Please see the below code,i hope it solves the issue
var res = [
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = res.reduce((a,c) => ({category:"Category All", max: a.max + c.max, min: a.min + c.min}))
console.log("result", result)
add a comment |
You could use Array.reduce, so the array of objects can be reduced into a single object, and using es6 you can write in a single line.
Please see the below code,i hope it solves the issue
var res = [
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = res.reduce((a,c) => ({category:"Category All", max: a.max + c.max, min: a.min + c.min}))
console.log("result", result)
You could use Array.reduce, so the array of objects can be reduced into a single object, and using es6 you can write in a single line.
Please see the below code,i hope it solves the issue
var res = [
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = res.reduce((a,c) => ({category:"Category All", max: a.max + c.max, min: a.min + c.min}))
console.log("result", result)
var res = [
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = res.reduce((a,c) => ({category:"Category All", max: a.max + c.max, min: a.min + c.min}))
console.log("result", result)
var res = [
{
category: 'Category A',
max: 10,
min: 12
},
{
category: 'Category B',
max: 2,
min: 1
},
{
category: 'Category C',
max: 4,
min: 4
}
]
var result = res.reduce((a,c) => ({category:"Category All", max: a.max + c.max, min: a.min + c.min}))
console.log("result", result)
answered Nov 12 '18 at 14:46
DILEEP THOMAS
950513
950513
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264342%2freduce-multiple-objects-into-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
"... with success.", you say.
– ibrahim mahrir
Nov 12 '18 at 14:34
Is the issue in the fact you do not understand reduce, or are you getting errors?
– Dellirium
Nov 12 '18 at 14:35
Is there any algorithm behind that reduction? Or are the numbers and the category label drawn by random?
– Nico Haase
Nov 12 '18 at 14:36
@NicoHaase pretty sure its a simple sum
– Dellirium
Nov 12 '18 at 14:37