按ID将对象的Javascript数组分组 [英] Group Javascript array of objects by ID

查看:118
本文介绍了按ID将对象的Javascript数组分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现的是,如果ID的前两位匹配,则将对象在数组内分组在一起.因此,鉴于下面的对象数组,我想转换此原始数组:

What I want to achieve is to group objects together inside an array if the first two digits of the IDs matches. So given the below array of objects, I want to convert this 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的前两位相同,则将消息添加到这些对象中,如在新阵列"中所见.

The key thing to note is that if the first two digits of the IDs are identical, then add the messages in those objects together as seen in the NEW ARRAY.

大家都可以看到,@ Nenad Vracar已经证明了对上述挑战的正确回答,但是,我还有另一个问题涉及另一个数组:

As you can all see, @Nenad Vracar has demonstrated a decent answer to the above challenge, however, I have a further question involving yet another array:

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

我要在其中拉出'candidateName'并将其作为数组添加到原始数组"result"中.

in which I want to pull the 'candidateName' and add it as an array to the original array, "result".

我已经在data1.reduce循环中尝试过一个data2.forEach循环,但是它似乎挂起了.

I've tried a data2.forEach loop inside the data1.reduce loop but it seems to hang.

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;
}, [])

我想知道是否有一种更优雅,更精致的方法不会被破坏?

I wonder if there's a more elegant, sophisticated method that will not break?

推荐答案

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

You can use reduce() to return array and one object for grouping.

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)

这篇关于按ID将对象的Javascript数组分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆