【问题标题】:Combined Array of object properties inside Object, Inside Array [duplicate]对象内部对象属性的组合数组,内部数组[重复]
【发布时间】:2021-03-12 11:14:28
【问题描述】:

给定这个对象数组:

[
    {"Code": 101, "Description": "Invoice"},
    {"Code": 102, "Description": "Credit"},
    {"Code": 103, "Description": "Credit"},
    {"Code": 111, "Description": "Invoice"},
    {"Code": 112, "Description": "Credit"},
    {"Code": 113, "Description": "Credit"},
    {"Code": 182, "Description": "Backup"}
]

我需要将匹配Descriptions 的对象组合成一个带有Codes 数组的单个对象,如下所示:

[
    {"Code": [101,111], "Description": "Invoice"},
    {"Code": [102,103,112,113], "Description": "Credit"},
    {"Code": [182], "Description": "Backup"}
]

我已尝试使用下面的 groupBy() 辅助函数(香草 JS 函数),但它不起作用,因为我得到了一个带有单个 undefined 属性的 Object,其原始数组的值为:

var groupBy = function (arr, criteria) {
    return arr.reduce(function (obj, item) {

        // Check if the criteria is a function to run on the item or a property of it
        var key = typeof criteria === 'function' ? criteria(item) : item[criteria];

        // If the key doesn't exist yet, create it
        if (!obj.hasOwnProperty(key)) {
            obj[key] = [];
        }

        // Push the value to the object
        obj[key].push(item);

        // Return the object to the next item in the loop
        return obj;

    }, {});
};

var array = [
    {"Code": 101, "Description": "Invoice"},
    {"Code": 102, "Description": "Credit"},
    {"Code": 103, "Description": "Credit"},
    {"Code": 111, "Description": "Invoice"},
    {"Code": 112, "Description": "Credit"},
    {"Code": 113, "Description": "Credit"},
    {"Code": 182, "Description": "Backup"}
];

var groupDescription = groupBy(array, array.forEach(document => document.Description));

console.log(groupDescription);

实现预期输出的最佳方式是什么?

【问题讨论】:

  • “最好的方法” - 你在找什么?当前的代码不起作用吗?
  • Mods:OP 之前曾问过这个问题,原始问题被否决并作为欺骗关闭。但是,欺骗链接上接受的答案并没有回答问题......
  • 感谢@RobinMackenzie 的澄清!
  • @evolutionxbox 不,它没有做我需要的。不过,罗宾的回答确实如此

标签: javascript arrays json object


【解决方案1】:

您的预期输出是Array,但groupBy 函数中的reduce 逻辑将Object 作为其初始值。不清楚为什么要为输入数组中的每个项目运行 criteria 函数,因为 DescriptionString,而不是 Array

一种更简单的方法(可能是最好的,也可能不是):

  • 以数组形式获取唯一的 Descriptions 集
  • 迭代该数组并针对每个Description,过滤原始数组并获得Codes

见下文:

var arr = [
  {"Code": 101, "Description": "Invoice"},
  {"Code": 102, "Description": "Credit"},
  {"Code": 103, "Description": "Credit"},
  {"Code": 111, "Description": "Invoice"},
  {"Code": 112, "Description": "Credit"},
  {"Code": 113, "Description": "Credit"},
  {"Code": 182, "Description": "Backup"}
];

// expected output
//[
//  {"Code": [101,111], "Description": "Invoice"},
//  {"Code": [102,103,112,113], "Description": "Credit"},
//  {"Code": [182], "Description": "Backup"}
//]

var grouped = Array
  .from(new Set(arr.map(o => o.Description))) // get unique Descriptions as array
  .map(d => { // iterate that array
    return {
      "Code": arr
        .filter(o => o.Description === d) 
        .map(o => o.Code), // return just the Code for original array items matching this unique Description
      "Description": d  // the Description
    }
  });
  
console.log(grouped);

【讨论】:

  • 太棒了!现在这完全符合我的需要。非常感谢,罗宾。感谢您向 Mods 澄清这是一个不同的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多