按ID分组Javascript对象数组

时间:2016-09-22 09:14:00

标签: javascript arrays

我想要实现的是如果ID的前两位匹配,则将对象组合在一个数组中。所以给定下面的对象数组,我想转换这个ORIGINAL ARRAY:

[
    {
        "id": "0100",
        "name": "name 1",
        "message": "Lorem blah blah 1"
    },
    {
        "id": "0101",
        "name": "",
        "message": "Lorem blah blah 1.1"
    },
    {
        "id": "0200",
        "name": "name 2",
        "message": "Lorem blah blah 2"
    },
    {
        "id": "0201",
        "name": "",
        "message": "Lorem blah blah 2.1"
    },
    {
        "id": "0202",
        "name": "",
        "message": "Lorem blah blah 2.2"
    },
    {
        "id": "0300",
        "name": "name 3",
        "message": "Lorem blah blah 3"
    },
    {
        "id": "0301",
        "name": "",
        "message": "Lorem blah blah 3.1"
    },
    {
        "id": "0302",
        "name": "",
        "message": "Lorem blah blah 3.2"
    },
    {
        "id": "0303",
        "name": "",
        "message": "Lorem blah blah 3.3"
    },
    {
        "id": "0304",
        "name": "",
        "message": "Lorem blah blah 3.4"
    }
]

进入这个新阵列:

[
    {
        "id": "0100",
        "name": "name 1",
        "message": [
            "Lorem blah blah 1",
            "Lorem blah blah 1.1"
        ]
    },
    {
        "id": "0200",
        "name": "name 2",
        "message": [
            "Lorem blah blah 2",
            "Lorem blah blah 2.1",
            "Lorem blah blah 2.2"
        ]
    },
    {
        "id": "0300",
        "name": "name 3",
        "message": [
            "Lorem blah blah 3",
            "Lorem blah blah 3.1",
            "Lorem blah blah 3.2",
            "Lorem blah blah 3.3",
            "Lorem blah blah 3.4"
        ]
    }
]

需要注意的关键是,如果ID的前两位数字相同,则将这些对象中的消息一起添加到NEW ARRAY中。

大家都知道,@ Nenad Vracar已经展示了对上述挑战的正确答案,但是,我还有另一个涉及另一个阵列的问题:

`var data2 = [{"CandidateName": "Mary", "relatedId": ["0100"]},{ "CandidateName": "John", "relatedId": ["0200"]},{ "CandidateName":"Peter", "relatedId": ["0300"]},{ "CandidateName": "Paul", "relatedId": ["0300"]}];`

其中我想拉出候选名称'并将其作为数组添加到原始数组中,"结果"。

我在data1.reduce循环中尝试了一个data2.forEach循环,但它似乎挂了。

var result = data.reduce(function(r, el) {

  // THIS INNER LOOP MAKES THE BROWSER HANG!!!
  data2.forEach(function (a){
    console.log('a',a); 
  });

  var e = el.id.slice(0, 2);
  if (!o[e]) {
    o[e] = {
      id: el.id,
      name: el.name,
      message: []
    }
    r.push(o[e]);
  }
  o[e].message.push(el.message);
  return r;
}, [])

我想知道是否有更优雅,更复杂的方法不会破坏?

2 个答案:

答案 0 :(得分:4)

您可以使用reduce()返回数组和一个对象进行分组。

var data = [{"id":"0100","name":"name 1","message":"Lorem blah blah 1"},{"id":"0101","name":"","message":"Lorem blah blah 1.1"},{"id":"0200","name":"name 2","message":"Lorem blah blah 2"},{"id":"0201","name":"","message":"Lorem blah blah 2.1"},{"id":"0202","name":"","message":"Lorem blah blah 2.2"},{"id":"0300","name":"name 3","message":"Lorem blah blah 3"},{"id":"0301","name":"","message":"Lorem blah blah 3.1"},{"id":"0302","name":"","message":"Lorem blah blah 3.2"},{"id":"0303","name":"","message":"Lorem blah blah 3.3"},{"id":"0304","name":"","message":"Lorem blah blah 3.4"}];

var o = {}
var result = data.reduce(function(r, el) {
  var e = el.id.slice(0, 2);
  if (!o[e]) {
    o[e] = {
      id: el.id,
      name: el.name,
      message: []
    }
    r.push(o[e]);
  }
  o[e].message.push(el.message);
  return r;
}, [])

console.log(result)

答案 1 :(得分:1)

您可以检查双零并将新组添加到结果集中。

var data = [{ "id": "0100", "name": "name 1", "message": "Lorem blah blah 1" }, { "id": "0101", "name": "", "message": "Lorem blah blah 1.1" }, { "id": "0200", "name": "name 2", "message": "Lorem blah blah 2" }, { "id": "0201", "name": "", "message": "Lorem blah blah 2.1" }, { "id": "0202", "name": "", "message": "Lorem blah blah 2.2" }, { "id": "0300", "name": "name 3", "message": "Lorem blah blah 3" }, { "id": "0301", "name": "", "message": "Lorem blah blah 3.1" }, { "id": "0302", "name": "", "message": "Lorem blah blah 3.2" }, { "id": "0303", "name": "", "message": "Lorem blah blah 3.3" }, { "id": "0304", "name": "", "message": "Lorem blah blah 3.4" }],
    grouped = [];

data.forEach(function (a) {
    var key = a.id.slice(0, 2);
    if (a.id === key + '00') {
        this[key] = { id: a.id, name: a.name, message: [] };
        grouped.push(this[key]);
    }
    this[key].message.push(a.message);
}, Object.create(null));

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }