In javascript, need to perform sum of dynamic array
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a dynamic array and I need to perform a sum of it.
{ClientName: "A", Alex: 2, Da: 0, Cal: 0}
{ClientName: "b", Alex: 0, Da: 0, Cal: 4}
{ClientName: "c", Alex: 1, Da: 0, Cal: 5}
{ClientName: "d", Alex: 2, Da: 0, Cal: 0}
in this array, client name is fixed but other columns like Alex, Da, Cal are dynamic and will vary. i need to create a generic function in typescript where output will be like this.
{ClientName: "Total", Alex: 5, Da: 0, Cal: 9}
javascript angular
add a comment |
I have a dynamic array and I need to perform a sum of it.
{ClientName: "A", Alex: 2, Da: 0, Cal: 0}
{ClientName: "b", Alex: 0, Da: 0, Cal: 4}
{ClientName: "c", Alex: 1, Da: 0, Cal: 5}
{ClientName: "d", Alex: 2, Da: 0, Cal: 0}
in this array, client name is fixed but other columns like Alex, Da, Cal are dynamic and will vary. i need to create a generic function in typescript where output will be like this.
{ClientName: "Total", Alex: 5, Da: 0, Cal: 9}
javascript angular
8
please add an attempt.
– Nina Scholz
Jan 4 at 12:51
1
“Columns are dynamic” means the property names are arbitrary and could be anything? Or is there a certain set of possible column names?
– deceze♦
Jan 4 at 12:57
add a comment |
I have a dynamic array and I need to perform a sum of it.
{ClientName: "A", Alex: 2, Da: 0, Cal: 0}
{ClientName: "b", Alex: 0, Da: 0, Cal: 4}
{ClientName: "c", Alex: 1, Da: 0, Cal: 5}
{ClientName: "d", Alex: 2, Da: 0, Cal: 0}
in this array, client name is fixed but other columns like Alex, Da, Cal are dynamic and will vary. i need to create a generic function in typescript where output will be like this.
{ClientName: "Total", Alex: 5, Da: 0, Cal: 9}
javascript angular
I have a dynamic array and I need to perform a sum of it.
{ClientName: "A", Alex: 2, Da: 0, Cal: 0}
{ClientName: "b", Alex: 0, Da: 0, Cal: 4}
{ClientName: "c", Alex: 1, Da: 0, Cal: 5}
{ClientName: "d", Alex: 2, Da: 0, Cal: 0}
in this array, client name is fixed but other columns like Alex, Da, Cal are dynamic and will vary. i need to create a generic function in typescript where output will be like this.
{ClientName: "Total", Alex: 5, Da: 0, Cal: 9}
javascript angular
javascript angular
edited Jan 4 at 13:24
Nurbol Alpysbayev
4,9091634
4,9091634
asked Jan 4 at 12:49
RajeshRajesh
4017
4017
8
please add an attempt.
– Nina Scholz
Jan 4 at 12:51
1
“Columns are dynamic” means the property names are arbitrary and could be anything? Or is there a certain set of possible column names?
– deceze♦
Jan 4 at 12:57
add a comment |
8
please add an attempt.
– Nina Scholz
Jan 4 at 12:51
1
“Columns are dynamic” means the property names are arbitrary and could be anything? Or is there a certain set of possible column names?
– deceze♦
Jan 4 at 12:57
8
8
please add an attempt.
– Nina Scholz
Jan 4 at 12:51
please add an attempt.
– Nina Scholz
Jan 4 at 12:51
1
1
“Columns are dynamic” means the property names are arbitrary and could be anything? Or is there a certain set of possible column names?
– deceze♦
Jan 4 at 12:57
“Columns are dynamic” means the property names are arbitrary and could be anything? Or is there a certain set of possible column names?
– deceze♦
Jan 4 at 12:57
add a comment |
5 Answers
5
active
oldest
votes
You can try something like this:
Idea:
- Uses
Object.keys
to get all enumerable keys. - Loop over them ans set them to default if not found. Then add current object's value.
- Since you need hardcoded value for
ClientName
, you can set it either in loop or set it after loop.
const data = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0},
]
const output = data.reduce((acc, item) => {
Object
.keys(item)
.forEach((key) => {
acc[key] = (acc[key] || 0) + item[key]
});
acc.ClientName = 'Total'
return acc;
}, {})
console.log(output)
add a comment |
You could reduce the wanted values.
var data = [{ ClientName: "A", Alex: 2, Da: 0, Cal: 0 }, { ClientName: "b", Alex: 0, Da: 0, Cal: 4 }, { ClientName: "c", Alex: 1, Da: 0, Cal: 5 }, { ClientName: "d", Alex: 2, Da: 0, Cal: 0 }],
total = data.reduce((a, { ClientName, ...b }) => Object.assign(
{},
a,
{ ClientName: 'total' },
...Object.entries(b).map(([k, v]) => ({ [k]: (a[k] || 0) + v }))
));
console.log(total);
add a comment |
Something along these lines should work as long as all keys besides ClientName are defined as numbers
arr.reduce((sumObject, item) => {
Object.keys(item).forEach(key => {
if(key === 'ClientName'){
return false
}
if(!sumObject[key]) {
sumObject[key] = item[key]
} else {
sumObject[key] += item[key]
}
})
return sumObject
}, {ClientName: 'Total'})
add a comment |
You can iterate with reduce
to generate single output and to merge the key count you can use Object.keys
and map
will allow you to iterate over your object keys. Also added one dynamic key Abc: 9
just to verify the result.
const object = [{
ClientName: "A",
Alex: 2,
Da: 0,
Cal: 0
},
{
ClientName: "b",
Alex: 0,
Da: 0,
Cal: 4
},
{
ClientName: "c",
Alex: 1,
Da: 0,
Cal: 5
},
{
ClientName: "d",
Alex: 2,
Da: 0,
Cal: 0,
Abc: 9
},
]
const result = object.reduce((accumulator, currentValue) => {
Object.keys(currentValue).map((indexKey) => {
accumulator[indexKey] = (accumulator[indexKey] || 0) + currentValue[indexKey]
});
accumulator.ClientName = "Total";
return accumulator;
})
console.log(result);
Your solution is exactly same as mine. Please check other answers before posting
– Rajesh
Jan 4 at 13:59
Yes I do agree, but It seems similar but not exactly, See in you code there isforEach
which is less optimized than.map
which is being used in my solution. Anyways I have already voted up for your solution.
– Rohit.007
Jan 4 at 14:11
using a map without return statement is so much worse. Also i don't think map is more optimised compared to forEach but that discussion can be done later
– Rajesh
Jan 4 at 14:13
add a comment |
let arr = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0}
];
let output = arr.reduce((curr, next) => {
return {ClientName: "Total", Alex: (curr.Alex + next.Alex ), Da: (curr.Da + next.Da), Cal: (curr.Cal + next.Cal)};
});
console.log(output);
Check this can get the answer simply
OP is looking for a solutions that handles Dynamic columns
– Rajesh
Jan 4 at 13:09
yes you are right , it shoud not work for dynamic columns.thank you,
– loghi aha
Jan 4 at 13:23
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%2f54039316%2fin-javascript-need-to-perform-sum-of-dynamic-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try something like this:
Idea:
- Uses
Object.keys
to get all enumerable keys. - Loop over them ans set them to default if not found. Then add current object's value.
- Since you need hardcoded value for
ClientName
, you can set it either in loop or set it after loop.
const data = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0},
]
const output = data.reduce((acc, item) => {
Object
.keys(item)
.forEach((key) => {
acc[key] = (acc[key] || 0) + item[key]
});
acc.ClientName = 'Total'
return acc;
}, {})
console.log(output)
add a comment |
You can try something like this:
Idea:
- Uses
Object.keys
to get all enumerable keys. - Loop over them ans set them to default if not found. Then add current object's value.
- Since you need hardcoded value for
ClientName
, you can set it either in loop or set it after loop.
const data = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0},
]
const output = data.reduce((acc, item) => {
Object
.keys(item)
.forEach((key) => {
acc[key] = (acc[key] || 0) + item[key]
});
acc.ClientName = 'Total'
return acc;
}, {})
console.log(output)
add a comment |
You can try something like this:
Idea:
- Uses
Object.keys
to get all enumerable keys. - Loop over them ans set them to default if not found. Then add current object's value.
- Since you need hardcoded value for
ClientName
, you can set it either in loop or set it after loop.
const data = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0},
]
const output = data.reduce((acc, item) => {
Object
.keys(item)
.forEach((key) => {
acc[key] = (acc[key] || 0) + item[key]
});
acc.ClientName = 'Total'
return acc;
}, {})
console.log(output)
You can try something like this:
Idea:
- Uses
Object.keys
to get all enumerable keys. - Loop over them ans set them to default if not found. Then add current object's value.
- Since you need hardcoded value for
ClientName
, you can set it either in loop or set it after loop.
const data = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0},
]
const output = data.reduce((acc, item) => {
Object
.keys(item)
.forEach((key) => {
acc[key] = (acc[key] || 0) + item[key]
});
acc.ClientName = 'Total'
return acc;
}, {})
console.log(output)
const data = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0},
]
const output = data.reduce((acc, item) => {
Object
.keys(item)
.forEach((key) => {
acc[key] = (acc[key] || 0) + item[key]
});
acc.ClientName = 'Total'
return acc;
}, {})
console.log(output)
const data = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0},
]
const output = data.reduce((acc, item) => {
Object
.keys(item)
.forEach((key) => {
acc[key] = (acc[key] || 0) + item[key]
});
acc.ClientName = 'Total'
return acc;
}, {})
console.log(output)
answered Jan 4 at 12:58
RajeshRajesh
16.7k52254
16.7k52254
add a comment |
add a comment |
You could reduce the wanted values.
var data = [{ ClientName: "A", Alex: 2, Da: 0, Cal: 0 }, { ClientName: "b", Alex: 0, Da: 0, Cal: 4 }, { ClientName: "c", Alex: 1, Da: 0, Cal: 5 }, { ClientName: "d", Alex: 2, Da: 0, Cal: 0 }],
total = data.reduce((a, { ClientName, ...b }) => Object.assign(
{},
a,
{ ClientName: 'total' },
...Object.entries(b).map(([k, v]) => ({ [k]: (a[k] || 0) + v }))
));
console.log(total);
add a comment |
You could reduce the wanted values.
var data = [{ ClientName: "A", Alex: 2, Da: 0, Cal: 0 }, { ClientName: "b", Alex: 0, Da: 0, Cal: 4 }, { ClientName: "c", Alex: 1, Da: 0, Cal: 5 }, { ClientName: "d", Alex: 2, Da: 0, Cal: 0 }],
total = data.reduce((a, { ClientName, ...b }) => Object.assign(
{},
a,
{ ClientName: 'total' },
...Object.entries(b).map(([k, v]) => ({ [k]: (a[k] || 0) + v }))
));
console.log(total);
add a comment |
You could reduce the wanted values.
var data = [{ ClientName: "A", Alex: 2, Da: 0, Cal: 0 }, { ClientName: "b", Alex: 0, Da: 0, Cal: 4 }, { ClientName: "c", Alex: 1, Da: 0, Cal: 5 }, { ClientName: "d", Alex: 2, Da: 0, Cal: 0 }],
total = data.reduce((a, { ClientName, ...b }) => Object.assign(
{},
a,
{ ClientName: 'total' },
...Object.entries(b).map(([k, v]) => ({ [k]: (a[k] || 0) + v }))
));
console.log(total);
You could reduce the wanted values.
var data = [{ ClientName: "A", Alex: 2, Da: 0, Cal: 0 }, { ClientName: "b", Alex: 0, Da: 0, Cal: 4 }, { ClientName: "c", Alex: 1, Da: 0, Cal: 5 }, { ClientName: "d", Alex: 2, Da: 0, Cal: 0 }],
total = data.reduce((a, { ClientName, ...b }) => Object.assign(
{},
a,
{ ClientName: 'total' },
...Object.entries(b).map(([k, v]) => ({ [k]: (a[k] || 0) + v }))
));
console.log(total);
var data = [{ ClientName: "A", Alex: 2, Da: 0, Cal: 0 }, { ClientName: "b", Alex: 0, Da: 0, Cal: 4 }, { ClientName: "c", Alex: 1, Da: 0, Cal: 5 }, { ClientName: "d", Alex: 2, Da: 0, Cal: 0 }],
total = data.reduce((a, { ClientName, ...b }) => Object.assign(
{},
a,
{ ClientName: 'total' },
...Object.entries(b).map(([k, v]) => ({ [k]: (a[k] || 0) + v }))
));
console.log(total);
var data = [{ ClientName: "A", Alex: 2, Da: 0, Cal: 0 }, { ClientName: "b", Alex: 0, Da: 0, Cal: 4 }, { ClientName: "c", Alex: 1, Da: 0, Cal: 5 }, { ClientName: "d", Alex: 2, Da: 0, Cal: 0 }],
total = data.reduce((a, { ClientName, ...b }) => Object.assign(
{},
a,
{ ClientName: 'total' },
...Object.entries(b).map(([k, v]) => ({ [k]: (a[k] || 0) + v }))
));
console.log(total);
answered Jan 4 at 13:13
Nina ScholzNina Scholz
199k15112182
199k15112182
add a comment |
add a comment |
Something along these lines should work as long as all keys besides ClientName are defined as numbers
arr.reduce((sumObject, item) => {
Object.keys(item).forEach(key => {
if(key === 'ClientName'){
return false
}
if(!sumObject[key]) {
sumObject[key] = item[key]
} else {
sumObject[key] += item[key]
}
})
return sumObject
}, {ClientName: 'Total'})
add a comment |
Something along these lines should work as long as all keys besides ClientName are defined as numbers
arr.reduce((sumObject, item) => {
Object.keys(item).forEach(key => {
if(key === 'ClientName'){
return false
}
if(!sumObject[key]) {
sumObject[key] = item[key]
} else {
sumObject[key] += item[key]
}
})
return sumObject
}, {ClientName: 'Total'})
add a comment |
Something along these lines should work as long as all keys besides ClientName are defined as numbers
arr.reduce((sumObject, item) => {
Object.keys(item).forEach(key => {
if(key === 'ClientName'){
return false
}
if(!sumObject[key]) {
sumObject[key] = item[key]
} else {
sumObject[key] += item[key]
}
})
return sumObject
}, {ClientName: 'Total'})
Something along these lines should work as long as all keys besides ClientName are defined as numbers
arr.reduce((sumObject, item) => {
Object.keys(item).forEach(key => {
if(key === 'ClientName'){
return false
}
if(!sumObject[key]) {
sumObject[key] = item[key]
} else {
sumObject[key] += item[key]
}
})
return sumObject
}, {ClientName: 'Total'})
answered Jan 4 at 12:59
Dennis RuiterDennis Ruiter
18528
18528
add a comment |
add a comment |
You can iterate with reduce
to generate single output and to merge the key count you can use Object.keys
and map
will allow you to iterate over your object keys. Also added one dynamic key Abc: 9
just to verify the result.
const object = [{
ClientName: "A",
Alex: 2,
Da: 0,
Cal: 0
},
{
ClientName: "b",
Alex: 0,
Da: 0,
Cal: 4
},
{
ClientName: "c",
Alex: 1,
Da: 0,
Cal: 5
},
{
ClientName: "d",
Alex: 2,
Da: 0,
Cal: 0,
Abc: 9
},
]
const result = object.reduce((accumulator, currentValue) => {
Object.keys(currentValue).map((indexKey) => {
accumulator[indexKey] = (accumulator[indexKey] || 0) + currentValue[indexKey]
});
accumulator.ClientName = "Total";
return accumulator;
})
console.log(result);
Your solution is exactly same as mine. Please check other answers before posting
– Rajesh
Jan 4 at 13:59
Yes I do agree, but It seems similar but not exactly, See in you code there isforEach
which is less optimized than.map
which is being used in my solution. Anyways I have already voted up for your solution.
– Rohit.007
Jan 4 at 14:11
using a map without return statement is so much worse. Also i don't think map is more optimised compared to forEach but that discussion can be done later
– Rajesh
Jan 4 at 14:13
add a comment |
You can iterate with reduce
to generate single output and to merge the key count you can use Object.keys
and map
will allow you to iterate over your object keys. Also added one dynamic key Abc: 9
just to verify the result.
const object = [{
ClientName: "A",
Alex: 2,
Da: 0,
Cal: 0
},
{
ClientName: "b",
Alex: 0,
Da: 0,
Cal: 4
},
{
ClientName: "c",
Alex: 1,
Da: 0,
Cal: 5
},
{
ClientName: "d",
Alex: 2,
Da: 0,
Cal: 0,
Abc: 9
},
]
const result = object.reduce((accumulator, currentValue) => {
Object.keys(currentValue).map((indexKey) => {
accumulator[indexKey] = (accumulator[indexKey] || 0) + currentValue[indexKey]
});
accumulator.ClientName = "Total";
return accumulator;
})
console.log(result);
Your solution is exactly same as mine. Please check other answers before posting
– Rajesh
Jan 4 at 13:59
Yes I do agree, but It seems similar but not exactly, See in you code there isforEach
which is less optimized than.map
which is being used in my solution. Anyways I have already voted up for your solution.
– Rohit.007
Jan 4 at 14:11
using a map without return statement is so much worse. Also i don't think map is more optimised compared to forEach but that discussion can be done later
– Rajesh
Jan 4 at 14:13
add a comment |
You can iterate with reduce
to generate single output and to merge the key count you can use Object.keys
and map
will allow you to iterate over your object keys. Also added one dynamic key Abc: 9
just to verify the result.
const object = [{
ClientName: "A",
Alex: 2,
Da: 0,
Cal: 0
},
{
ClientName: "b",
Alex: 0,
Da: 0,
Cal: 4
},
{
ClientName: "c",
Alex: 1,
Da: 0,
Cal: 5
},
{
ClientName: "d",
Alex: 2,
Da: 0,
Cal: 0,
Abc: 9
},
]
const result = object.reduce((accumulator, currentValue) => {
Object.keys(currentValue).map((indexKey) => {
accumulator[indexKey] = (accumulator[indexKey] || 0) + currentValue[indexKey]
});
accumulator.ClientName = "Total";
return accumulator;
})
console.log(result);
You can iterate with reduce
to generate single output and to merge the key count you can use Object.keys
and map
will allow you to iterate over your object keys. Also added one dynamic key Abc: 9
just to verify the result.
const object = [{
ClientName: "A",
Alex: 2,
Da: 0,
Cal: 0
},
{
ClientName: "b",
Alex: 0,
Da: 0,
Cal: 4
},
{
ClientName: "c",
Alex: 1,
Da: 0,
Cal: 5
},
{
ClientName: "d",
Alex: 2,
Da: 0,
Cal: 0,
Abc: 9
},
]
const result = object.reduce((accumulator, currentValue) => {
Object.keys(currentValue).map((indexKey) => {
accumulator[indexKey] = (accumulator[indexKey] || 0) + currentValue[indexKey]
});
accumulator.ClientName = "Total";
return accumulator;
})
console.log(result);
const object = [{
ClientName: "A",
Alex: 2,
Da: 0,
Cal: 0
},
{
ClientName: "b",
Alex: 0,
Da: 0,
Cal: 4
},
{
ClientName: "c",
Alex: 1,
Da: 0,
Cal: 5
},
{
ClientName: "d",
Alex: 2,
Da: 0,
Cal: 0,
Abc: 9
},
]
const result = object.reduce((accumulator, currentValue) => {
Object.keys(currentValue).map((indexKey) => {
accumulator[indexKey] = (accumulator[indexKey] || 0) + currentValue[indexKey]
});
accumulator.ClientName = "Total";
return accumulator;
})
console.log(result);
const object = [{
ClientName: "A",
Alex: 2,
Da: 0,
Cal: 0
},
{
ClientName: "b",
Alex: 0,
Da: 0,
Cal: 4
},
{
ClientName: "c",
Alex: 1,
Da: 0,
Cal: 5
},
{
ClientName: "d",
Alex: 2,
Da: 0,
Cal: 0,
Abc: 9
},
]
const result = object.reduce((accumulator, currentValue) => {
Object.keys(currentValue).map((indexKey) => {
accumulator[indexKey] = (accumulator[indexKey] || 0) + currentValue[indexKey]
});
accumulator.ClientName = "Total";
return accumulator;
})
console.log(result);
answered Jan 4 at 13:29
Rohit.007Rohit.007
1,7892521
1,7892521
Your solution is exactly same as mine. Please check other answers before posting
– Rajesh
Jan 4 at 13:59
Yes I do agree, but It seems similar but not exactly, See in you code there isforEach
which is less optimized than.map
which is being used in my solution. Anyways I have already voted up for your solution.
– Rohit.007
Jan 4 at 14:11
using a map without return statement is so much worse. Also i don't think map is more optimised compared to forEach but that discussion can be done later
– Rajesh
Jan 4 at 14:13
add a comment |
Your solution is exactly same as mine. Please check other answers before posting
– Rajesh
Jan 4 at 13:59
Yes I do agree, but It seems similar but not exactly, See in you code there isforEach
which is less optimized than.map
which is being used in my solution. Anyways I have already voted up for your solution.
– Rohit.007
Jan 4 at 14:11
using a map without return statement is so much worse. Also i don't think map is more optimised compared to forEach but that discussion can be done later
– Rajesh
Jan 4 at 14:13
Your solution is exactly same as mine. Please check other answers before posting
– Rajesh
Jan 4 at 13:59
Your solution is exactly same as mine. Please check other answers before posting
– Rajesh
Jan 4 at 13:59
Yes I do agree, but It seems similar but not exactly, See in you code there is
forEach
which is less optimized than .map
which is being used in my solution. Anyways I have already voted up for your solution.– Rohit.007
Jan 4 at 14:11
Yes I do agree, but It seems similar but not exactly, See in you code there is
forEach
which is less optimized than .map
which is being used in my solution. Anyways I have already voted up for your solution.– Rohit.007
Jan 4 at 14:11
using a map without return statement is so much worse. Also i don't think map is more optimised compared to forEach but that discussion can be done later
– Rajesh
Jan 4 at 14:13
using a map without return statement is so much worse. Also i don't think map is more optimised compared to forEach but that discussion can be done later
– Rajesh
Jan 4 at 14:13
add a comment |
let arr = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0}
];
let output = arr.reduce((curr, next) => {
return {ClientName: "Total", Alex: (curr.Alex + next.Alex ), Da: (curr.Da + next.Da), Cal: (curr.Cal + next.Cal)};
});
console.log(output);
Check this can get the answer simply
OP is looking for a solutions that handles Dynamic columns
– Rajesh
Jan 4 at 13:09
yes you are right , it shoud not work for dynamic columns.thank you,
– loghi aha
Jan 4 at 13:23
add a comment |
let arr = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0}
];
let output = arr.reduce((curr, next) => {
return {ClientName: "Total", Alex: (curr.Alex + next.Alex ), Da: (curr.Da + next.Da), Cal: (curr.Cal + next.Cal)};
});
console.log(output);
Check this can get the answer simply
OP is looking for a solutions that handles Dynamic columns
– Rajesh
Jan 4 at 13:09
yes you are right , it shoud not work for dynamic columns.thank you,
– loghi aha
Jan 4 at 13:23
add a comment |
let arr = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0}
];
let output = arr.reduce((curr, next) => {
return {ClientName: "Total", Alex: (curr.Alex + next.Alex ), Da: (curr.Da + next.Da), Cal: (curr.Cal + next.Cal)};
});
console.log(output);
Check this can get the answer simply
let arr = [
{ClientName: "A", Alex: 2, Da: 0, Cal: 0},
{ClientName: "b", Alex: 0, Da: 0, Cal: 4},
{ClientName: "c", Alex: 1, Da: 0, Cal: 5},
{ClientName: "d", Alex: 2, Da: 0, Cal: 0}
];
let output = arr.reduce((curr, next) => {
return {ClientName: "Total", Alex: (curr.Alex + next.Alex ), Da: (curr.Da + next.Da), Cal: (curr.Cal + next.Cal)};
});
console.log(output);
Check this can get the answer simply
answered Jan 4 at 13:07
loghi ahaloghi aha
114
114
OP is looking for a solutions that handles Dynamic columns
– Rajesh
Jan 4 at 13:09
yes you are right , it shoud not work for dynamic columns.thank you,
– loghi aha
Jan 4 at 13:23
add a comment |
OP is looking for a solutions that handles Dynamic columns
– Rajesh
Jan 4 at 13:09
yes you are right , it shoud not work for dynamic columns.thank you,
– loghi aha
Jan 4 at 13:23
OP is looking for a solutions that handles Dynamic columns
– Rajesh
Jan 4 at 13:09
OP is looking for a solutions that handles Dynamic columns
– Rajesh
Jan 4 at 13:09
yes you are right , it shoud not work for dynamic columns.thank you,
– loghi aha
Jan 4 at 13:23
yes you are right , it shoud not work for dynamic columns.thank you,
– loghi aha
Jan 4 at 13:23
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.
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%2f54039316%2fin-javascript-need-to-perform-sum-of-dynamic-array%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
8
please add an attempt.
– Nina Scholz
Jan 4 at 12:51
1
“Columns are dynamic” means the property names are arbitrary and could be anything? Or is there a certain set of possible column names?
– deceze♦
Jan 4 at 12:57