gpt4 book ai didi

javascript - 将子项的多个值相加并将结果保存到 Javascript 中 n 叉树中的父项

转载 作者:行者123 更新时间:2023-11-30 14:19:50 25 4
gpt4 key购买 nike

我问这个:Sum children values and save result to parent in n-ary tree in Javascript

我收到了@Nina Scholz 的回复。现在我正在尝试使用相同的代码但具有多个值,例如:

value: {A: 1, B: 2, C: 3}

我试过这样的:

function update(array) {
var root = [],
references = array.reduce((r, o) => {
if (!o.parents.length) {
root.push(o.id);
}
r[o.id] = o;
return r;
},Object.create(null));

root.reduce(
function sum(s, id) {
var o = references[id];

for (var key in o.value) {
return s + (o.value[key] = o.children.reduce(sum, 0) || o.value[key])
}
}, 0);

return array;
}

var data2 = [{ id: 'A', parents: [], children: ['B', 'E'], value: {A:1, B:2} }, { id: 'B', parents: ['A'], children: ['C', 'D'], value: {A:1, B:2} }, { id: 'C', parents: ['B'], children: [], value: {A:1, B:2} }, { id: 'D', parents: ['B'], children: [], value: {A:1, B:2} }, { id: 'E', parents: ['A'], children: ['F'], value: {A:1, B:2} }, { id: 'F', parents: ['E'], children: [], value: {A:1, B:2} }]

console.log(update(data2));

但由于返回值的缘故,仅适用于值 A。

我尝试的另一件事:

function update(array) {
var root = [],
references = array.reduce((r, o) => {
if (!o.parents.length) {
root.push(o.id);
}
r[o.id] = o;
return r;
},Object.create(null));

root.reduce(
function sum(s, id) {
var o = references[id];

var x;
for (var key in o.value) {
x = (s + (o.value[key] = o.children.reduce(sum, 0) || o.value[key]));
}
}, 0);

return array;
}

var data2 = [{ id: 'A', parents: [], children: ['B', 'E'], value: {A:1, B:2} }, { id: 'B', parents: ['A'], children: ['C', 'D'], value: {A:1, B:2} }, { id: 'C', parents: ['B'], children: [], value: {A:1, B:2} }, { id: 'D', parents: ['B'], children: [], value: {A:1, B:2} }, { id: 'E', parents: ['A'], children: ['F'], value: {A:1, B:2} }, { id: 'F', parents: ['E'], children: [], value: {A:1, B:2} }]

console.log(update(data2));

但它不适用于有 child 的人。

最佳答案

您可以调整 sum 并迭代键/值以添加到 value 对象。

function update(array) {

function sum(s, id) {
var o = references[id];
o.value = o.children.reduce(sum, undefined) || o.value;
if (!s) {
return Object.assign({}, o.value);
}
Object
.entries(o.value)
.forEach(([k, v]) => s[k] = (s[k] || 0) + v);
return s;
}

var root = [],
references = array.reduce((r, o) => {
if (!o.parents.length) {
root.push(o.id);
}
r[o.id] = o;
return r;
}, Object.create(null));

root.reduce(sum, undefined);

return array;
}

var data2 = [{ id: 'A', parents: [], children: ['B', 'E'], value: { A: 1, B: 2 } }, { id: 'B', parents: ['A'], children: ['C', 'D'], value: { A: 1, B: 2 } }, { id: 'C', parents: ['B'], children: [], value: { A: 1, B: 2 } }, { id: 'D', parents: ['B'], children: [], value: { A: 1, B: 2 } }, { id: 'E', parents: ['A'], children: ['F'], value: { A: 1, B: 2 } }, { id: 'F', parents: ['E'], children: [], value: { A: 1, B: 2 } }]
console.log(update(data2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 将子项的多个值相加并将结果保存到 Javascript 中 n 叉树中的父项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52949711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com