对象数组使用lodash映射到网格(表)?

时间:2017-04-20 11:48:57

标签: javascript arrays lodash

所以,假设我有一个很好的平面对象数组:

let workers = [{
    name: 'Tom',
    age: 20,
    town: 'Paris'
    gender: 'M',
    position: 'DEV'
},{
    name: 'Mike',
    age: 21,
    town: 'Paris'
    gender: 'M',
    position: 'DEV'
},{
    name: 'Jake',
    age: 20,
    town: 'London'
    gender: 'M',
    position: 'DEV'
},{
    name: 'Simona',
    age: 25,
    town: 'Rome'
    gender: 'F',
    position: 'DEV'
},{
    name: 'Manuela',
    age: 20,
    town: 'Rome'
    gender: 'F',
    position: 'DEV'
},{
    name: 'Angelo',
    age: 22,
    town: 'Milan'
    gender: 'M',
    position: 'PR'
},{
    name: 'Oliver',
    age: 30,
    town: 'Berlin'
    gender: 'M',
    position: 'PR'
}];

我想根据我想要的属性创建一个网格(行和列)。

  

array2gridAll(array,rowProperty,colPropery)

let rows = array2gridAll(workers, 'position', 'town');

所以网格就像这样

|----------------------------------------------------------------------------|--------------|
|               |    Berlin   |    London    |     Milan     |     Paris     |     Rome     |
|----------------------------------------------------------------------------|--------------|
|               |      {}     |    Jake{}    |       {}      |     Tom{}     |    Simona{}  |
|       DEV     |------------------------------------------------------------|--------------|
|               |      {}     |      {}      |       {}      |     Mike{}    |   Manuela{}  |
|----------------------------------------------------------------------------|--------------|
|       PR      |   Oliver{}  |      {}      |    Angelo{}   |       {}      |      {}      |
|-------------------------------------------------------------------------------------------|

所以实际数组:

rows = [
    [
        {

        },{
            name: 'Jake',
            age: 20,
            town: 'London'
            gender: 'M',
            position: 'DEV'
        {

        },{
            name: 'Tom',
            age: 20,
            town: 'Paris'
            gender: 'M',
            position: 'DEV'
        },{
            name: 'Simona',
            age: 25,
            town: 'Rome'
            gender: 'F',
            position: 'DEV'
        }
    ],
    [
        {

        },{

        },{

        },{
            name: 'Mike',
            age: 21,
            town: 'Paris'
            gender: 'M',
            position: 'DEV'
        },{
            name: 'Manuela',
            age: 20,
            town: 'Rome'
            gender: 'F',
            position: 'DEV'
        },
    ],
    [
        {
            name: 'Oliver',
            age: 30,
            town: 'Berlin'
            gender: 'M',
            position: 'PR'
        },{

        },{
            name: 'Angelo',
            age: 22,
            town: 'Milan'
            gender: 'M',
            position: 'PR'
        },{

        },{

        }
    ]
];

希望它有意义吗?

另一个功能是

  

array2grid(array,rowProperty,rows,colPropery,cols)

let rows = array2grid(workers, 'position', ['DEV'], 'town', ['Rome', 'Paris']);

我会去哪里

|------------------------------------------------|
|               |      Rome     |     Paris      |
|------------------------------------------------|
|               |     Simona{}  |     Tom{}      |
|       DEV     |--------------------------------|
|               |    Manuela{}  |     Mike{}     |
|------------------------------------------------|

rows = [
    [{
        name: 'Simona',
        age: 25,
        town: 'Rome'
        gender: 'F',
        position: 'DEV'
    },{
        name: 'Tom',
        age: 20,
        town: 'Paris'
        gender: 'M',
        position: 'DEV'
    }],
    [{
        name: 'Manuela',
        age: 20,
        town: 'Rome'
        gender: 'F',
        position: 'DEV'
    },{
        name: 'Mike',
        age: 21,
        town: 'Paris'
        gender: 'M',
        position: 'DEV'
    }]
];

令人尴尬的是,在我将这个问题放在一起的时候,我可以将自己发展成自我:)

但想知道最干净的方法吗?

1 个答案:

答案 0 :(得分:0)

没有lodash。没有完整的表格。但是达到结构化结果的两种方法。

  1. 带有名称的数组,作为其他数据的代理。
  2. 反映关卡的对象。
  3. 为什么是阵列?它直接工作,但它不是非常通用,因为只过滤所需的行/列。

    具有分层结构的对象更容易创建并过滤以仅获取所需位置或城镇。

    function getGrid(array, rowKey, colKey) {
        var grid = [],
            cols = Object.create(null),
            rows = Object.create(null),
            colLength = 0,
            rowLength = 0;
    
        array.forEach(function (a) {
            var row, col;
            if (!(a[rowKey] in rows)) {
                rows[a[rowKey]] = rowLength++;
            }
            if (!(a[colKey] in cols)) {
                cols[a[colKey]] = colLength++;
            }
            row = rows[a[rowKey]];
            col = cols[a[colKey]];
    
            grid[row] = grid[row] || [];
            if (col in grid[row]) {
                if (!Array.isArray(grid[row][col])) {
                    grid[row][col] = [grid[row][col]];
                }
                grid[row][col].push(a.name);
            } else {
                grid[row][col] = a.name;
            }
        });
        return grid;
    }
    
    function getObject(array, rowKey, colKey) {
        var object = {};
    
        array.forEach(function (a) {
            object[a[rowKey]] = object[a[rowKey]] || {};
            object[a[rowKey]][a[colKey]] = object[a[rowKey]][a[colKey]] || [];
            object[a[rowKey]][a[colKey]].push(a.name);
        });
    
        return object;
    }
    
    var workers = [{ name: 'Tom', age: 20, town: 'Paris', gender: 'M', position: 'DEV' }, { name: 'Mike', age: 21, town: 'Paris', gender: 'M', position: 'DEV' }, { name: 'Jake', age: 20, town: 'London', gender: 'M', position: 'DEV' }, { name: 'Simona', age: 25, town: 'Rome', gender: 'F', position: 'DEV' }, { name: 'Manuela', age: 20, town: 'Rome', gender: 'F', position: 'DEV' }, { name: 'Angelo', age: 22, town: 'Milan', gender: 'M', position: 'PR' }, { name: 'Oliver', age: 30, town: 'Berlin', gender: 'M', position: 'PR' }];
    
    console.log(getGrid(workers, 'position', 'town'));
    console.log(getObject(workers, 'position', 'town'));
    .as-console-wrapper { max-height: 100% !important; top: 0; }