以下是具有根据我的业务需求更改行cols的逻辑的代码。当我保留此代码时,在我的Angular2 ts代码中,我得到的提供的参数与.forEach(...

var data = [{ Id: "a", ColumnLocation: "0", RowLocation: "0" }, { Id: "b", ColumnLocation: "0", RowLocation: "1" }, { Id: "4", ColumnLocation: "0", RowLocation: "3" }, { Id: "c", ColumnLocation: "1", RowLocation: "0" }, { Id: "d", ColumnLocation: "1", RowLocation: "2" }, { Id: "e", ColumnLocation: "2", RowLocation: "0" }, { Id: "e", ColumnLocation: "2", RowLocation: "2" }];

data.forEach(function (col, row) {
                return function (o) {
                    if (col !== o.ColumnLocation) {
                        col = o.ColumnLocation;
                        row = 0;
                    }
                    if (+o.RowLocation !== row) {
                        o.RowLocation = row.toString();
                    }
                    row++;
                }
            }());

最佳答案

正如其他人提到的那样,为了对参数使用自执行功能,您需要提供这些参数。

let myCol; // or you can set this to an initial value
let myRow;

data.forEach(function (col, row) {
            return function (o) {
                // your logic here
            };
        }(myCol, myRow));


您的打字稿编译器在抱怨,因为您的函数带有两个参数colrow,但是您在调用时不带任何参数。

10-08 01:23