【问题标题】:Parsing this table with Javascript into array of objects使用 Javascript 将此表解析为对象数组
【发布时间】:2016-09-29 16:24:31
【问题描述】:

我有这个源表数据:

我想得到这样解析的数据(按行分组):

我将源表分组到一个对象数组中。 这是我的数组:

[ { rowId: 4, colId: 10 } { rowId: 4, colId: 11 } .... ]

现在我想得到一个已解析对象的数组..

我该怎么做? 我使用 for 循环解析了数组,但是在创建新的对象数组时出现了一些错误。

我的代码:

    for (var i=0; i<tableArray.length; i++) {

    if (tableArray[i].rowId != rowLast) {
        bar = true;     
        var row = tableArray[i].rowId;
        var start = tableArray[i].colId;
    }

    if  ((bar)&&(tableArray[i].colId != colLast)) {
        var end = tableArray[i].colId;
        tab = { row: row, start: start, end: end }
        newTableArray.push(tab);
        bar = false;
    }


    rowLast = tableArray[i].rowId;
    colLast = tableArray[i].colId;      

}

帮助!我在循环中有点困惑:(

非常感谢。

【问题讨论】:

  • 请贴出你的js代码
  • 问题不清楚
  • 如果我理解正确你想这样做: [ { rowId: 4, colId: 10 } { rowId: 4, colId: 11 } .... ] => [ { rowId: 4 ,开始:10,结束:12 } ....],对吗?
  • 贴出js代码。查看@user2415266 结果。这就是我想要的。

标签: javascript jquery arrays object html-table


【解决方案1】:

您可以对元素进行分组并为最后一个值使用一个对象。此解决方案需要排序数据。

var array = [{ rowId: 4, colId: 10 }, { rowId: 4, colId: 11 }, { rowId: 4, colId: 12 }, { rowId: 4, colId: 20 }, { rowId: 4, colId: 21 }, { rowId: 6, colId: 6 }, { rowId: 6, colId: 7 }, { rowId: 6, colId: 8 }, { rowId: 7, colId: 12 }, ],
    group = [];

array.forEach(function (a, i) {
    if (!i ||                          // group changes if first object i = 0
        this.last.row !== a.rowId ||   //               or different rowId
        this.last.end + 1 !== a.colId  //               or not in sequence
    ) {
        this.last = { row: a.rowId, start: a.colId, end: a.colId };
        group.push(this.last);
    }
    this.last.end = a.colId;
}, {});

console.log(group);

【讨论】:

    【解决方案2】:

    我宁愿写一个函数来生成新数组,希望cmets解释一下它背后的思路:

    function transform(array) {
        var output = [];
        // initiates the first object you want in your output array
        // with the row and colId of the first object from the input array
        var obj = {
            row: array[0].row,
            start: array[0].colId,
            end: array[0].colId
        };
        // Loop starts at 1 instead of 0 because we used the first object array[0] already
        for (var i = 1; i < array.length; i++) {
            var current = array[i];
            // if the current objects row is still the same,
            // AND the colId is the next colId (meaning no spare cols between)
            // set the new objects end to this colId
            if(obj.row === current.row && (current.colId - obj.end) === 1 ){
                obj.end = current.colId;
            }
            // when the row does not match, add the object to the output array and
            // re-innitiate it with the current objects row and colId
            else {
                output.push(obj);
                obj.row = current.row;
                obj.start = current.colId;
                obj.end = current.colId;
            }
        }
        // Once the loop is done, add the last remaining object to the output array
        output.push(obj);
        return output;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-18
      • 1970-01-01
      • 2016-07-08
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2022-11-24
      • 1970-01-01
      相关资源
      最近更新 更多