Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

javascript - How do i add a summed list in a recursive method?

I'm searching for a recursive javascriptmethod. I have this structure and i want to have the userlist in each section..

let sectionObject=
{
  anotherSection1: {
    section1:{
      userlist:[ {name:"user1", val:1},{name:"user2", val:1}]
    },
    section2:{
      userlist:[ {name:"user1", val:1}]
    }
 },
  anotherSection2: {
    section1:{
      userlist:[ {name:"user1", val:5},{name:"user2", val:3}]
    },
    section2:{
      userlist:[ {name:"user1", val:6}]
    }
  }
}

for example the anotherSection should have a userlist too. In this Userlist the values of the childelements are summed up. anotherSection has two child elements.. section1 and section2.. anotherSection.userlist should therefore be:

anotherSection.userlist: [ {name:"user1", val:2}, {name:"user2", val:1} ] (the user1 value of the child elements were added together).. sectionObject should have a userlist too.. i appreciate every help :)

This should be the outcome:

  let sectionObject=
{
  anotherSection1: {
    section1:{
      userlist:[ {name:"user1", val:1},{name:"user2", val:1}]
    },
    section2:{
      userlist:[ {name:"user1", val:1}]
    },
    userlist:[ {name:"user1", val:2},{name:"user2", val:1}]
  },
  anotherSection2: {
    section1:{
      userlist:[ {name:"user1", val:5},{name:"user2", val:3}]
    },
    section2:{
      userlist:[ {name:"user1", val:6}]
    },
    userlist:[ {name:"user1", val:11}, {name:"user2", val:3}]
  },
  userlist:[ {name:"user1", val:13}, {name:"user2", val:4}]
}

what i have tried is something like this...

function somefunc(obj){
    temp = {}
    let tempuserliste = [];
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
        obj[key].userlist.forEach(function(element) {
          if (typeof temp[element.name] === 'undefined'){
            temp[element.name] = {'name':element.name, 'val':element.val}
            tempuserlist.push(temp[element.name])
          } else {
            temp[element.name].val += element.val
          }
        });
      }
    }
    return tempuserlist;
  }

  sectionObject.anotherSection1.usertlist = somefunc(anotherSection1)

somefunc() works well on one anothersection.. but i don't know how to combine this in a recursive method...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To make the function recursive, just make it call itself on child objects if they don't already have a userlist property, and instead of returning tempuserlist at the end, it should just assign it directly to the object obj:

// ...
for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        if(!obj[key].hasOwnProperty("userlist")) {
            somefunc(obj[key]);
        }
// ...
obj.userlist = tempuserlist;

Demo:

function somefunc(obj) {
   let temp = {};  // this shouldn't be global (undeclared thus global) or you'll have some very serious problems with the recursion
   let tempuserlist = [];
   for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
         if(!obj[key].hasOwnProperty("userlist")) {
            somefunc(obj[key]);
         }
         obj[key].userlist.forEach(function(element) {
            if (!temp.hasOwnProperty(element.name)){
               tempuserlist.push(
                  temp[element.name] = { name: element.name, val: element.val }
               );
            } else {
               temp[element.name].val += element.val;
            }
         });
      }
   }
   obj.userlist = tempuserlist;
}

let sectionObject = {"anotherSection1":{"section1":{"userlist":[{"name":"user1","val":1},{"name":"user2","val":1}]},"section2":{"userlist":[{"name":"user1","val":1}]}},"anotherSection2":{"section1":{"userlist":[{"name":"user1","val":5},{"name":"user2","val":3}]},"section2":{"userlist":[{"name":"user1","val":6}]}}};

somefunc(sectionObject);

console.log(sectionObject);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...