Groupby不使用下划线js

时间:2015-06-18 14:49:30

标签: javascript jquery grouping

我有一个按ID分组的集合。每个id包含对象数组。我想循环遍历它并创建一个目标集合,该集合将按“Id”列分组,如示例所示。我不会使用下划线js。我必须使用javascript reduce方法来实现这一点。

var targetCollection = [{
    123456: [
        { "Id": "1", "name": "xxx", "age": "22" },
        { "Id": "1", "name": "yyy", "age": "15" },
        { "Id": "5", "name": "zzz", "age": "59" }
    ],
    789456: [
        { "Id": "1", "name": "xxx", "age": "22" },
        { "Id": "1", "name": "yyy", "age": "15" },
        { "Id": "5", "name": "zzz", "age": "59" }
    ]
}]

var targetOutput = [{
    123456: {
        1: [{ "Id": "1", "name": "xxx", "age": "22" }, { "Id": "1", "name": "yyy", "age": "15" }],
        5: [{ "Id": "5", "name": "zzz", "age": "59" }]
    },
    789456: {
        1: [{ "Id": "1", "name": "xxx", "age": "22" }, { "Id": "1", "name": "yyy", "age": "15" }],
        5: [{ "Id": "5", "name": "zzz", "age": "59" }]
    }
}]

Code Snippet I have tried which didn't work

var newArray = [];
$.each(targetCollection, function (key, value) {
    var newTarget = $(this);
    var targetCollectionNew = newTarget.reduce(function (result, current) {
        result[current.ParentAreaID] = result[current.ParentAreaID] || [];
        result[current.ParentAreaID].push(current);
        return result;
    }, {});

    newArray.push(targetCollectionNew);
});

console.log('newArray', newArray);

我尝试过使用array reduce方法,但它没有用。请帮我。

2 个答案:

答案 0 :(得分:3)

原始尝试的问题在于您需要有两个循环,因为在第一个newValue中,对象不是数组,因此没有reduce方法。

更简单的解决方案是使用map(您可以使用forEach$.each)并在第二个循环中减少(我使用简单for-in)。

这样的事情:

var targetCollection = [{
    123456: [
        { "Id": "1", "name": "xxx", "age": "22" },
        { "Id": "1", "name": "yyy", "age": "15" },
        { "Id": "5", "name": "zzz", "age": "59" }
    ],
    789456: [
        { "Id": "1", "name": "xxx", "age": "22" },
        { "Id": "1", "name": "yyy", "age": "15" },
        { "Id": "5", "name": "zzz", "age": "59" }
    ]
}];

var result = targetCollection.map(function(obj) {
    for (var key in obj) {
        obj[key] = obj[key].reduce(function(prev, curr) {
            if (!prev[curr.Id]) prev[curr.Id] = [];
            prev[curr.Id].push(curr);
            return prev;
        }, {});
    }
    return obj;
});

document.write('<pre>' + JSON.stringify(result, null, 4) + '</pre>');

答案 1 :(得分:2)

您应该比较当前对象targetCollection和所需的输出对象targetOutput,以规划所需的转换:

targetCollection: Array --> Object --> Array --> Object
targetObject: Array --> Object --> Object --> Array --> Object

您可以看到外部数组将是1:1映射,因此无需在此处进行缩减。

下一级是一个Object,但它又是一个1:1的映射。但是,因为JavaScript通过引用传递对象,所以您需要创建一个新对象以避免修改原始对象。由于我们正在从另一个对象创建一个对象,我们可以利用Object.keys的强大功能创建一个数组,然后使用Array.reduce从这些键创建一个对象。

第3级是我们第一次遇到从Array到Object的结构的真正转换。同样,我们可以使用Array.reduce来帮助这一步。

因此,根据此攻击计划,您可以看到流程应为map --> reduce --> reduce

&#13;
&#13;
var targetCollection = [{
  123456: [{
    "Id": "1",
    "name": "xxx",
    "age": "22"
  }, {
    "Id": "1",
    "name": "yyy",
    "age": "15"
  }, {
    "Id": "5",
    "name": "zzz",
    "age": "59"
  }],
  789456: [{
    "Id": "1",
    "name": "xxx",
    "age": "22"
  }, {
    "Id": "1",
    "name": "yyy",
    "age": "15"
  }, {
    "Id": "5",
    "name": "zzz",
    "age": "59"
  }]
}];

// 1:1 mapping
var targetOutput = targetCollection.map(function(obj) {
  // 1:1 mapping but we need to create a new object to
  //   avoid modifying the original object
  // Object.keys returns an array of keys (that are enumerable)
  //   and then reduce creates the new object
  return Object.keys(obj).reduce(function(newObj, key) {
    // The desired transformation takes place here
    newObj[key] = obj[key].reduce(function(idObj, el) {
      idObj[el.Id] = idObj[el.Id] || [];
      // This will pass by reference the internal objects
      //   so you could use the prior technique to create
      //   a new object to avoid downstream modification
      idObj[el.Id].push(el);

      return idObj;
    }, {});

    return newObj;
  }, {});
});

document.write('<h3>targetCollection</h3><pre>' + JSON.stringify(targetCollection, null, 4) + '</pre>');
document.write('<h3>targetOutput</h3><pre>' + JSON.stringify(targetOutput, null, 4) + '</pre>');
&#13;
&#13;
&#13;