基于多个键在JavaScript中对数据进行分组

3

收到 JSON 响应

[{
    "name": "A1",
    "date": "2019-03-13",
    "comment": "xyz"
  },
  {
    "name": "A1",
    "date": "2019-03-13",
    "comment": "abc"
  },
  {
    "name": "B1",
    "date": "2019-03-13",
    "comment": "pqr"
  },
  {
    "name": "A1",
    "date": "2019-03-14",
    "comment": "mno"
  }
]

期望输出

[
  {
    "name": "A1",
    "date": "2019-03-13",
    "data": [
      {
        "name": "A1",
        "date": "2019-03-13",
        "comment": "xyz"
      },
      {
        "name": "A1",
        "date": "2019-03-13",
        "comment": "abc"
      }
    ],
    {
      "name": "A1",
      "date": "2019-03-14",
      "data": [
        {
          "name": "A1",
          "date": "2019-03-14",
          "comment": "mno"
        }
      ]
    },
    {
      "name": "B1",
      "date": "2019-03-13",
      "data": [
        {
          "name": "B1",
          "date": "2019-03-13",
          "comment": "pqr"
        }
      ]
    }
  ]

我想创建一个包含唯一名称和日期及其相应数据的新javascript数组。

有人可以帮帮我吗?


3
为什么会出现数据重复? - mplungjan
4个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
3
您可以使用array#reduce来基于对象累加器中的namedate对日期进行分组,然后使用Object.values()提取所有值。

let data = [{ "name": "A1", "date": "2019-03-13", "comment": "xyz" }, { "name": "A1", "date": "2019-03-13", "comment": "abc" }, { "name": "B1", "date": "2019-03-13", "comment": "pqr" }, { "name": "A1", "date": "2019-03-14", "comment": "mno" } ],
    result = Object.values(data.reduce((r,{name,date, comment}) => {
      r[name + "_" + date] = r[name + "_" + date] || {name, date, data: []};
      r[name + "_" + date].data.push({name,date,comment});
      return r;
    },{}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }


我花了更多的时间来发布这个问题,与你如此迅速的回应相比。非常感谢!!! - Krunal Shah

3
你可以使用一个数组来进行所需的分组,并查找对象。

var data = [{ name: "A1", date: "2019-03-13", comment: "xyz" }, { name: "A1", date: "2019-03-13", comment: "abc" }, { name: "B1", date: "2019-03-13", comment: "pqr" }, { name: "A1", date: "2019-03-14", comment: "mno" }],
    groupBy = ['name', 'date'],
    grouped = data.reduce((r, o) => {
        var temp = r.find(p => groupBy.every(k => o[k] === p[k]));
        if (!temp) {
            r.push(temp = Object.assign(...groupBy.map(k => ({ [k]: o[k] })), { data: [] }));
        }
        temp.data.push(o);
        return r;
    }, []);

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


2
你可以使用reduce来收集这些值:

var input = [{
    "name": "A1",
    "date": "2019-03-13",
    "comment": "xyz"
  },
  {
    "name": "A1",
    "date": "2019-03-13",
    "comment": "abc"
  },
  {
    "name": "B1",
    "date": "2019-03-13",
    "comment": "pqr"
  },
  {
    "name": "A1",
    "date": "2019-03-14",
    "comment": "mno"
  }
]

var res = input.reduce((acc, {name, date, comment}) => {
    var found = acc.find(el => el.name === name && el.date === date);
    return found 
        ? found.data.push({name, date, comment}) && acc 
        : [...acc, {name, date, data: [{name, date, comment}]}];
}, []);

console.log(JSON.stringify(res));


0

一个简单的方法是:

const data = [{
    "name": "A1",
    "date": "2019-03-13",
    "comment": "xyz"
  },
  {
    "name": "A1",
    "date": "2019-03-13",
    "comment": "abc"
  },
   {
    "name": "A1",
    "date": "2019-03-13",
    "comment": "fffffffff"
  },
  {
    "name": "B1",
    "date": "2019-03-13",
    "comment": "pqr"
  },
   {
    "name": "B1",
    "date": "2019-03-13",
    "comment": "hhhhhhhhhhh"
  },
  {
    "name": "A1",
    "date": "2019-03-14",
    "comment": "mno"
  }
]


const res = data.reduce((all, acc) => {

  const found = all.find(o => o.name === acc.name && o.date === acc.date)

  if (found === undefined) {
    all.push(acc)
  } else {
    found.data = (found.data || []).concat(acc)
  }

  return all;

}, [])

console.log(res)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,