Reputation: 1582
How find sum of all value in that object? In object included array with another object with value and may be "next" array with similar structure object.
{
value: 4,
next: [
{
value: 3,
next: [...]
},
{
value: 3,
next: [...]
},
...
]
}
Upvotes: 0
Views: 326
Reputation: 572
The structure of your object is called tree Tree (data structure). You can use breadth-first or depth-first approach to traverse the tree and gather the sum along the way.
Other answers say you have to go recursively but you can do it iteratively with the breadth-first approach, I show you both approaches down in the code snippet.
I've expanded your data sample adding null values where there is supposedely no next values (you could use any kind of check really).
let data = {
value: 4,
next: [
{
value: 3,
next: [
{
value: 5,
next: null
},
{
value: 6,
next: null
}
]
},
{
value: 2,
next: [
{
value: 2,
next: null
},
{
value: 3,
next: [
{
value: 7,
next: null
},
{
value: 8,
next: null
}
]
}
]
}
]
}
// iterative approach
function breadthFirst(node){
let sum = 0
let q = [node]
while(q.length > 0){
let node = q.pop()
sum += node.value
if (node.next !== null) {
for (let other of node.next){
q.push(other)
}
}
}
return sum
}
console.log('breadthFirst sum (iterative):', breadthFirst(data))
// recursive apporach
function depthFirst(node){
let sum = 0
function recursion(node){
sum += node.value
if (node.next !== null) {
for (let other of node.next){
recursion(other)
}
}
}
recursion(node)
return sum
}
console.log('depthFirst sum (recursive):', depthFirst(data))
Upvotes: 1
Reputation: 13964
Use reduce and Object.entries to sum the values recursively:
const obj = {
value: 4,
next: [{
value: 3,
next: [{ value: 1 }]
}, {
value: 3,
next: [{ value: 2, next: [] }]
}]
};
const sum = (obj) =>
Object.entries(obj).reduce((acc, [k, v]) => acc + (
k === 'next'
? v.map(sum).reduce((s, x) => s + x, 0)
: k === 'value' ? v : 0)
, 0);
console.log(sum(obj));
Upvotes: 2
Reputation: 350137
You would need recursion to deal with the arbitrary nesting of your object:
const nestedSum = o => (o.next || []).reduce((acc, o) => acc + nestedSum(o), o.value);
// Demo
const data = {
value: 4,
next: [{
value: 3,
next: [{value: 5}]
}, {
value: 3,
next: []
},
]
};
console.log(nestedSum(data));
Upvotes: 5
Reputation: 867
function sum(obj, current = 0) {
const nextSum = (obj.next || []).reduce((nextSum, obj) => nextSum + sum(obj, current), 0)
return current + nextSum + obj.value
}
const example = {
value: 4,
next: [
{
value: 3,
next: [{
value: 7
}]
},
{
value: 3
}
]
}
console.log(sum(example)) // must be 17
Upvotes: 2
Reputation: 50291
You need a recursive function and check if the value of a key is a number then add with the variable , else if it is an array like next
then iterate through it and again call the same function with a new object
let data = {
value: 4,
next: [{
value: 3,
next: [{
value: 4
}, {
value: 5
}]
},
{
value: 3,
next: [{
value: 2
}]
}
]
}
let sum = 0;
function findSum(obj) {
for (let keys in obj) {
if (typeof obj[keys] === 'number') {
sum += obj[keys];
} else if (Array.isArray(obj[keys]) && obj[keys].length > 0) {
obj[keys].forEach(function(item) {
findSum(item)
})
}
}
}
findSum(data);
console.log(sum)
Upvotes: 0