gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:33:03 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com