将多个值求和,并将结果保存到Java语言的n元树中

时间:2018-10-23 12:58:23

标签: javascript arrays recursion

我问了这个: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));

但不适用于有孩子的人。

1 个答案:

答案 0 :(得分:2)

您可以调整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; }