如何以递归方法添加汇总列表?

时间:2018-10-15 00:02:50

标签: javascript arrays json recursion

我正在搜索一种递归javascript方法。我具有这种结构,并且我希望每个部分都有userlist

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}]
    }
  }
}

例如anotherSection也应该有一个userlist。 在此用户列表中,汇总了子元素的值。 anotherSection有两个子元素。.section1section2 .. anotherSection.userlist应该是:

anotherSection.userlist:[{名称:“ user1”,值:2},{名称:“ user2”,值:1}] (子元素的user1值加在一起了。)sectionObject也应该有一个userlist ..感谢您的帮助:)

这应该是结果:

  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}]
}

我尝试过的是这样的...

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()在一个anothersection上可以很好地工作..但是我不知道如何在递归方法中将其组合起来...

1 个答案:

答案 0 :(得分:1)

要使该函数具有递归性,只需使其在子对象上调用自身(如果它们没有userlist属性),而不是在最后返回tempuserlist,而应将其赋值直接到对象obj

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

演示:

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);