Dynamically calculate the average for a nested collection using lodash

前端 未结 4 536
予麋鹿
予麋鹿 2021-01-27 17:03

I have a JSON array of objects (a collection) like:

[{
  \"x\": {
        \"x1\": 1
  },
  \"y\": {
    \"yt\": 0,
    \"zt\": 4,
    \"qa\": 3,
    \"ft\": 0,
          


        
4条回答
  •  臣服心动
    2021-01-27 17:24

    var array = [{
      "x": {
            "x1": 1
      },
      "y": {
        "yt": 0,
        "zt": 4,
        "qa": 3,
        "ft": 0
      }
    },
    {
      "x": {
            "x1": 5
      },
      "y": {
        "yt": 10,
        "zt": 2,
        "qa": 0,
        "ft": 0
      }
    }];
    function aintob(){
      var o = {};
      var first = array[0],
          second = array[1];
      var result = {x:{},y:{}};
      var each = function(letter, oa, ob){
        var i,
        letter = {};
        for(i in oa){
          letter[i] = (oa[i]+ob[i])/2;
        }
        return letter;
      }
      o.x = each("x", first.x, second.x);
      o.y = each("y", first.y, second.y);
      return o;
    }
    console.log(aintob());

提交回复
热议问题