为了账号安全,请及时绑定邮箱和手机立即绑定

不使用 For 循环处理任何数组

不使用 For 循环处理任何数组

MMTTMM 2023-03-24 15:15:13
我想在不使用FOR循环的情况下动态实现输出。输入是多个对象的数组。var input = [  {    "name": "Siner1",    "RTime": 40,    "FTime": 30  },  {    "name": "Siner2",    "RTime": 50,    "FTime": 60  }]var output = [  {    "RTime": {      "Siner1": 40,      "Siner2": 50    },    "FTime": {      "Siner1": 30,      "Siner2": 60    }  }]//console.log(input)console.log(output);有人可以帮助我吗?
查看完整描述

2 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

方法一

var input = [

  {

    "name": "Siner1",

    "RTime": 40,

    "FTime": 30

  },

  {

    "name": "Siner2",

    "RTime": 50,

    "FTime": 60

  }

]


var output = input.reduce(function(initial,next){

   initial["RTime"][next["name"]]=(initial["RTime"][next["name"]] || 0 ) + next["RTime"];

   initial["FTime"][next["name"]]=(initial["FTime"][next["name"]] || 0 ) + next["FTime"];

   return initial;

},{"RTime":{},"FTime":{}});




console.log(output)

方法二

如果您想要聚合其他键或值


var input = [

  {

    "name": "Siner1",

    "RTime": 40,

    "FTime": 30

  },

  {

    "name": "Siner2",

    "RTime": 50,

    "FTime": 60

  }

]


var keys = ['RTime','FTime'];

output=input.reduce(function(initial,next){

   keys.map(function(key){

        initial[key][next["name"]]=(initial[key][next["name"]] || 0 ) + next[key];

   });

   return initial;

},keys.reduce(function(data,key){

   data[key]={};

   return data;

},{}));


console.log(output)


查看完整回答
反对 回复 2023-03-24
?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

提供的通用方法基于Array.prototype.reduce. 由于它使用accumulator其回调函数的参数作为可配置项,collector因此它完全不知道任何给定列表的任何已处理项目的键。只需要提供已处理项目的(源)键值,然后作为新分组键值的目标键...


function restructureKeysAndValues(collector, item) {

  const {

    keyOfTargetValue,

    registry,

    index,

    list

  } = collector;

  const targetKey = item[keyOfTargetValue];


  Object.entries(item).forEach(([key, value]) => {

    if (key !== keyOfTargetValue) {


      let keyGroup = registry[key];

      if (!keyGroup) {

        keyGroup = registry[key] = { [key]: {} };


        Object.assign(index, keyGroup);

        list.push(keyGroup);

      }

      keyGroup[key][targetKey] = value;

    }

  });

  return collector;

}


console.log('a list of group objects ...', [{


    "name": "Siner1",

    "RTime": 40,

    "FTime": 30

  }, {

    "name": "Siner2",

    "RTime": 50,

    "FTime": 60


  }].reduce(restructureKeysAndValues, {


    keyOfTargetValue: 'name',

    registry: {}, // - for internal tracking

    index: {},    // - for external use, output as map/index. 

    list: []      // - for external use, output as array/list.


  }).list

);


console.log('a single grouped object ...', [{


    "name": "Siner1",

    "RTime": 40,

    "FTime": 30

  }, {

    "name": "Siner2",

    "RTime": 50,

    "FTime": 60


  }].reduce(restructureKeysAndValues, {


    keyOfTargetValue: 'name',

    registry: {}, // - for internal tracking

    index: {},    // - for external use, output as map/index. 

    list: []      // - for external use, output as array/list.


  }).index

);


console.log('a list of group objects ...', [{


    "name": "Siner1",

    "RTime": 40,

    "FTime": 30,

    "CTime": 70,

    "ATime": 90

  }, {

    "name": "Siner2",

    "RTime": 50,

    "FTime": 60,

    "CTime": 30,

    "ATime": 40

  }, {

    "name": "Siner3",

    "RTime": 90,

    "FTime": 20,

    "CTime": 10,

    "ATime": 20


  }].reduce(restructureKeysAndValues, {


    keyOfTargetValue: 'name',

    registry: {}, // - for internal tracking

    index: {},    // - for external use, output as map/index. 

    list: []      // - for external use, output as array/list.


  }).list

);

.as-console-wrapper { min-height: 100%!important; top: 0; }


查看完整回答
反对 回复 2023-03-24
  • 2 回答
  • 0 关注
  • 88 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信