javascript - 通过识别缺失值重新创建 Json

标签 javascript ecmascript-6 underscore.js lodash

我有一个 Json 数据,在将其发送到我的组件之前需要对其进行调整。我的 Json 如下。我需要识别缺失的字段并将下面的字段向上移动。

[{
  "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"
}]

我所需的 Json 是:

[{
  "Id": "a",
  "ColumnLocation": "0",
  "RowLocation": "0"
}, {
  "Id": "b",
  "ColumnLocation": "0",
  "RowLocation": "1"
},
{
  "Id": "4",
  "ColumnLocation": "0",
  "RowLocation": "2"
},
 {
  "Id": "c",
  "ColumnLocation": "1",
  "RowLocation": "0"
}, {
  "Id": "d",
  "ColumnLocation": "1",
  "RowLocation": "1"
}, {
  "Id": "e",
  "ColumnLocation": "2",
  "RowLocation": "0"
},
 {
  "Id": "e",
  "ColumnLocation": "2",
  "RowLocation": "1"
}]

这里在 (0,0), (0,1) 之后,属性 (0,2) 丢失,所以我需要将其向上移动并使其成为 (0,2).. 在 (1,0) 之后也是如此,属性 (1,1) 缺失,因此它必须是 (1,1)。

我尝试为此编写一个自定义函数,但无法实现它,因此认为任何适合此场景的 map 函数

我正在从 API 获取小工具信息。在某些情况下,某些小工具可能会丢失,因此我需要向上拉出该位置并绘制小工具。

this.userService.getGadgets(id).subscribe(gadgets => { this.res = gadgets.map(function (v) { return v.ColumnLocation; }); 

// required logic ************/ 
for (let gadget of gadgets) {
 this.dashboardsText = ""; 
switch (gadget.Name) {

最佳答案

您可以存储最后一列和行,然后检查它是否不是同一列,然后重置行计数器。

然后检查RowLocation是否等于行计数器,如果不等于则设置新值。

最后增加行计数器。

var array = [{ 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" }];

array.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++;
    }
}());

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

如果闭包,您可以使用全局变量,也许这对您有用。

var array = [{ 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" }],
    col,
    row;

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

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

关于javascript - 通过识别缺失值重新创建 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44465884/

相关文章:

javascript - 在 es6 for of 循环中使用 const

javascript - 带有 Socket.IO 的 NodeJS 延迟发送数据

javascript - JSHint 与 ECMAScript6 : method is not defined

javascript - 使用更改文本自动调整定义的 DIV 中的字体大小

javascript - 将 jQuery 与来自 ES6 的类一起使用

javascript - 为什么 UnderscoreJS 使用 toString.call() 而不是 typeof?

javascript - 在 Backbone Marionette 2.4.4 中将下划线 1.8.3 换成 lodash 4.2.1

javascript - 相当于 Lodash _.get 和 _.has 的下划线

javascript - 使用 javascript 渲染字符串模板的最有效方法

javascript - HTML 表单不会执行正确的操作,干扰