getting the total of a nested object property
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a an object as shown below, how can i get the total value of ca from each properties
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
sometimes the temp1 can consist of only one property as test1, so inorder to satisfy that case i thoguht of finding the first property of object. But when test2 test3 comes i can't use of 0. Also the property ca can exist or not.
temp1[Object.keys(temp1)[0]]
i have tried and i am getting the output , but can we improve this one
var arr =
Object.keys(temp1).forEach(o => {
Object.keys(temp1[o]).forEach(so => {
if(so === "ca"){
arr.push(temp1[o][so])
}
})
})
arr.reduce((a,b) => a + b)
expected output is
var total = 2
javascript
add a comment |
I have a an object as shown below, how can i get the total value of ca from each properties
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
sometimes the temp1 can consist of only one property as test1, so inorder to satisfy that case i thoguht of finding the first property of object. But when test2 test3 comes i can't use of 0. Also the property ca can exist or not.
temp1[Object.keys(temp1)[0]]
i have tried and i am getting the output , but can we improve this one
var arr =
Object.keys(temp1).forEach(o => {
Object.keys(temp1[o]).forEach(so => {
if(so === "ca"){
arr.push(temp1[o][so])
}
})
})
arr.reduce((a,b) => a + b)
expected output is
var total = 2
javascript
add a comment |
I have a an object as shown below, how can i get the total value of ca from each properties
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
sometimes the temp1 can consist of only one property as test1, so inorder to satisfy that case i thoguht of finding the first property of object. But when test2 test3 comes i can't use of 0. Also the property ca can exist or not.
temp1[Object.keys(temp1)[0]]
i have tried and i am getting the output , but can we improve this one
var arr =
Object.keys(temp1).forEach(o => {
Object.keys(temp1[o]).forEach(so => {
if(so === "ca"){
arr.push(temp1[o][so])
}
})
})
arr.reduce((a,b) => a + b)
expected output is
var total = 2
javascript
I have a an object as shown below, how can i get the total value of ca from each properties
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
sometimes the temp1 can consist of only one property as test1, so inorder to satisfy that case i thoguht of finding the first property of object. But when test2 test3 comes i can't use of 0. Also the property ca can exist or not.
temp1[Object.keys(temp1)[0]]
i have tried and i am getting the output , but can we improve this one
var arr =
Object.keys(temp1).forEach(o => {
Object.keys(temp1[o]).forEach(so => {
if(so === "ca"){
arr.push(temp1[o][so])
}
})
})
arr.reduce((a,b) => a + b)
expected output is
var total = 2
javascript
javascript
edited Jan 3 at 4:31
DILEEP THOMAS
asked Jan 3 at 4:21


DILEEP THOMASDILEEP THOMAS
1,1662616
1,1662616
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can reduce()
over the Object.values()
, you can catch the undefined ca
with a short circuit like ca || 0
-- it will use ca
if it's defined otherwise 0:
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
let sum = Object.values(temp1).reduce((sum,{ca}) => sum + (ca || 0), 0)
console.log(sum)
does it work if you add another deeper level?
– quirimmo
Jan 3 at 4:29
@quirimmo, not this just iterates over the values of the object. To go arbitrarily deeper would probably need to do some recursion.
– Mark Meyer
Jan 3 at 4:30
1
@MarkMeyer thanks for the single line code and for the quick response
– DILEEP THOMAS
Jan 3 at 4:33
add a comment |
Iterate through the object and check if the key is present. If it is so then add the value to a variable
var temp1 = {
test1: {
ca: 1,
ba: 2,
da: 3
},
test2: {
ca: 1,
ba: 2,
da: 3
},
test3: {
ba: 2,
da: 3
}
}
function valueOfKeyToGet(key) {
let val = 0;
for (let keys in temp1) {
if (temp1[keys][key] !== undefined) {
val += temp1[keys][key];
}
}
console.log(val)
}
valueOfKeyToGet('ca')
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%2f54016301%2fgetting-the-total-of-a-nested-object-property%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can reduce()
over the Object.values()
, you can catch the undefined ca
with a short circuit like ca || 0
-- it will use ca
if it's defined otherwise 0:
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
let sum = Object.values(temp1).reduce((sum,{ca}) => sum + (ca || 0), 0)
console.log(sum)
does it work if you add another deeper level?
– quirimmo
Jan 3 at 4:29
@quirimmo, not this just iterates over the values of the object. To go arbitrarily deeper would probably need to do some recursion.
– Mark Meyer
Jan 3 at 4:30
1
@MarkMeyer thanks for the single line code and for the quick response
– DILEEP THOMAS
Jan 3 at 4:33
add a comment |
You can reduce()
over the Object.values()
, you can catch the undefined ca
with a short circuit like ca || 0
-- it will use ca
if it's defined otherwise 0:
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
let sum = Object.values(temp1).reduce((sum,{ca}) => sum + (ca || 0), 0)
console.log(sum)
does it work if you add another deeper level?
– quirimmo
Jan 3 at 4:29
@quirimmo, not this just iterates over the values of the object. To go arbitrarily deeper would probably need to do some recursion.
– Mark Meyer
Jan 3 at 4:30
1
@MarkMeyer thanks for the single line code and for the quick response
– DILEEP THOMAS
Jan 3 at 4:33
add a comment |
You can reduce()
over the Object.values()
, you can catch the undefined ca
with a short circuit like ca || 0
-- it will use ca
if it's defined otherwise 0:
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
let sum = Object.values(temp1).reduce((sum,{ca}) => sum + (ca || 0), 0)
console.log(sum)
You can reduce()
over the Object.values()
, you can catch the undefined ca
with a short circuit like ca || 0
-- it will use ca
if it's defined otherwise 0:
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
let sum = Object.values(temp1).reduce((sum,{ca}) => sum + (ca || 0), 0)
console.log(sum)
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
let sum = Object.values(temp1).reduce((sum,{ca}) => sum + (ca || 0), 0)
console.log(sum)
var temp1 = {test1: {ca:1, ba:2, da: 3}, test2: {ca:1, ba:2, da: 3}, test3: { ba:2, da: 3}}
let sum = Object.values(temp1).reduce((sum,{ca}) => sum + (ca || 0), 0)
console.log(sum)
answered Jan 3 at 4:25


Mark MeyerMark Meyer
40.2k33464
40.2k33464
does it work if you add another deeper level?
– quirimmo
Jan 3 at 4:29
@quirimmo, not this just iterates over the values of the object. To go arbitrarily deeper would probably need to do some recursion.
– Mark Meyer
Jan 3 at 4:30
1
@MarkMeyer thanks for the single line code and for the quick response
– DILEEP THOMAS
Jan 3 at 4:33
add a comment |
does it work if you add another deeper level?
– quirimmo
Jan 3 at 4:29
@quirimmo, not this just iterates over the values of the object. To go arbitrarily deeper would probably need to do some recursion.
– Mark Meyer
Jan 3 at 4:30
1
@MarkMeyer thanks for the single line code and for the quick response
– DILEEP THOMAS
Jan 3 at 4:33
does it work if you add another deeper level?
– quirimmo
Jan 3 at 4:29
does it work if you add another deeper level?
– quirimmo
Jan 3 at 4:29
@quirimmo, not this just iterates over the values of the object. To go arbitrarily deeper would probably need to do some recursion.
– Mark Meyer
Jan 3 at 4:30
@quirimmo, not this just iterates over the values of the object. To go arbitrarily deeper would probably need to do some recursion.
– Mark Meyer
Jan 3 at 4:30
1
1
@MarkMeyer thanks for the single line code and for the quick response
– DILEEP THOMAS
Jan 3 at 4:33
@MarkMeyer thanks for the single line code and for the quick response
– DILEEP THOMAS
Jan 3 at 4:33
add a comment |
Iterate through the object and check if the key is present. If it is so then add the value to a variable
var temp1 = {
test1: {
ca: 1,
ba: 2,
da: 3
},
test2: {
ca: 1,
ba: 2,
da: 3
},
test3: {
ba: 2,
da: 3
}
}
function valueOfKeyToGet(key) {
let val = 0;
for (let keys in temp1) {
if (temp1[keys][key] !== undefined) {
val += temp1[keys][key];
}
}
console.log(val)
}
valueOfKeyToGet('ca')
add a comment |
Iterate through the object and check if the key is present. If it is so then add the value to a variable
var temp1 = {
test1: {
ca: 1,
ba: 2,
da: 3
},
test2: {
ca: 1,
ba: 2,
da: 3
},
test3: {
ba: 2,
da: 3
}
}
function valueOfKeyToGet(key) {
let val = 0;
for (let keys in temp1) {
if (temp1[keys][key] !== undefined) {
val += temp1[keys][key];
}
}
console.log(val)
}
valueOfKeyToGet('ca')
add a comment |
Iterate through the object and check if the key is present. If it is so then add the value to a variable
var temp1 = {
test1: {
ca: 1,
ba: 2,
da: 3
},
test2: {
ca: 1,
ba: 2,
da: 3
},
test3: {
ba: 2,
da: 3
}
}
function valueOfKeyToGet(key) {
let val = 0;
for (let keys in temp1) {
if (temp1[keys][key] !== undefined) {
val += temp1[keys][key];
}
}
console.log(val)
}
valueOfKeyToGet('ca')
Iterate through the object and check if the key is present. If it is so then add the value to a variable
var temp1 = {
test1: {
ca: 1,
ba: 2,
da: 3
},
test2: {
ca: 1,
ba: 2,
da: 3
},
test3: {
ba: 2,
da: 3
}
}
function valueOfKeyToGet(key) {
let val = 0;
for (let keys in temp1) {
if (temp1[keys][key] !== undefined) {
val += temp1[keys][key];
}
}
console.log(val)
}
valueOfKeyToGet('ca')
var temp1 = {
test1: {
ca: 1,
ba: 2,
da: 3
},
test2: {
ca: 1,
ba: 2,
da: 3
},
test3: {
ba: 2,
da: 3
}
}
function valueOfKeyToGet(key) {
let val = 0;
for (let keys in temp1) {
if (temp1[keys][key] !== undefined) {
val += temp1[keys][key];
}
}
console.log(val)
}
valueOfKeyToGet('ca')
var temp1 = {
test1: {
ca: 1,
ba: 2,
da: 3
},
test2: {
ca: 1,
ba: 2,
da: 3
},
test3: {
ba: 2,
da: 3
}
}
function valueOfKeyToGet(key) {
let val = 0;
for (let keys in temp1) {
if (temp1[keys][key] !== undefined) {
val += temp1[keys][key];
}
}
console.log(val)
}
valueOfKeyToGet('ca')
answered Jan 3 at 4:27
brkbrk
29.9k32246
29.9k32246
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.
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%2f54016301%2fgetting-the-total-of-a-nested-object-property%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