javascript - 使用对象对数组项进行分组将超过 2 个元素

标签 javascript arrays object

我找到了这个解决方案来解决我要解决的问题 here

唯一的区别是我的对象数组有两个以上的元素 并且想要的结果与解决方案相似,但包含所有元素

{
    "group": "one",
    "color": ["red", "green", "black"],
    "size": ["big"],
    "date": ["11/08/2018"]
}

所以我一直在重复 .map() 来显示我所有的值,但我觉得我不应该...
有人可以帮我提供更简单更好的选择吗?

var db = [{"Record":{"id":"26","cost_center":"15073 DC1 M8 - Filmatic","batch_no":"367746","item_code":"12583","description":"LF Fruited Guava (2x6)x15"}},{"Record":{"id":"29","cost_center":"15073 DC1 M8 - Filmatic","batch_no":"367749","item_code":"12583","description":"LF Fruited Guava (2x6)x15"}},{"Record":{"id":"36","cost_center":"15093 DC1 M10 - CornerPot Machi","batch_no":"367756","item_code":"12256","description":"PROMO CP LF SaltedCar w H"}}];

var myArray = [];
for (var i in db) {
    if (db.hasOwnProperty(i)) {
        myArray.push(db[i].Record);
    }
}

var res = myArray.reduce(function(res, elem) {
    if (res.indexOf(elem.cost_center) === -1) {
        res.push(elem.cost_center);
    }
    return res;
}, []).map(function(machine) {
    return {
        cost_center: machine,
        batch_no: myArray.filter(function(_el) {
            return _el.cost_center === machine;
        }).map(function(_el) { return _el.batch_no; }),
        item_code: myArray.filter(function(_el) {
            return _el.cost_center === machine;
        }).map(function(_el) { return _el.item_code; }),
        description: myArray.filter(function(_el) {
            return _el.cost_center === machine;
        }).map(function(_el) { return _el.description; })
    }
});

console.log(res);

最佳答案

对于后面添加的代码,您可以使用哈希表,在其中收集具有相同 cost_center 的所有对象,并使用另一个数组来收集属性的值,例如 batch_noitem_codedescription

var db = [{ Record: { id: "26", cost_center: "15073 DC1 M8 - Filmatic", batch_no: "367746", item_code: "12583", description: "LF Fruited Guava (2x6)x15" } }, { Record: { id: "29", cost_center: "15073 DC1 M8 - Filmatic", batch_no: "367749", item_code: "12583", description: "LF Fruited Guava (2x6)x15" } }, { Record: { id: "36", cost_center: "15093 DC1 M10 - CornerPot Machi", batch_no: "367756", item_code: "12256", description: "PROMO CP LF SaltedCar w H" } }],
    keys = ["batch_no", "item_code", "description"],
    hash = Object.create(null),
    result = [];

db.forEach(function (o) {
    if (!hash[o.Record.cost_center]) {
        hash[o.Record.cost_center] = { cost_center: o.Record.cost_center };
        keys.forEach(function (k) {
            hash[o.Record.cost_center][k] = [o.Record[k]];
        });
        result.push(hash[o.Record.cost_center]);
        return;
    }
    keys.forEach(function (k) {
        hash[o.Record.cost_center][k].push(o.Record[k]);
    });
});

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

关于javascript - 使用对象对数组项进行分组将超过 2 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45632490/

相关文章:

c++ - 关于c++中遍历数组的问题

ios - 为每个 TableViewCell 实例化新数组

javascript - 使用变量作为多维对象中的键

javascript - 从数据属性将字符串解析为对象

javascript - 在哪些浏览器中查询字符串(以防止缓存)有效?

javascript - ElectronJS : Drag and Drop and file onto the electron app icon on the Windows taskbar

wpf - 绑定(bind)到内联数组声明内的 DataContext 属性

javascript - for循环中的Object.defineProperty

javascript - 滑动菜单工作正常但有双重动画

Javascript:仅在未查看 Qualtrics 页面时单击 'Next'