通过识别缺少的值来重新创建Json [英] Recreate the Json by identifying the missing values
问题描述
我有一个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
}] $ b $ (0,0),(0,1)中,属性(0,2)缺少,所以我需要移动它,使它(0,2)..相同的方式(1,0)后,属性(1,1)丢失,所以它必须是(1,1)。
我尝试为此编写一个自定义函数,但无法实现它,所以认为适合这种情况的任何地图函数
我正在从API获取小工具信息。在某些情况下,某些小工具可能会丢失,因此我需要将该位置向上拉,并绘制小工具。
this.userService.getGadgets(id).subscribe(gadgets => {this.res = gadgets.map(function v){return v.ColumnLocation;});
//必需的逻辑************ /
(小工具的小工具){
this.dashboardsText =;
开关(gadget.Name){
解决方案您可以存储最后一列和行,然后检查是否不一致列,然后重置行计数器。
然后检查如果 RowLocation
等于行计数器,如果未设置新值。
最后增加行计数器。 data =truedata-bab =false>
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 !== o.ColumnLocation){col = o.ColumnLocation; row = 0; } if(+ o.RowLocation!== row){o.RowLocation = row.toString(); }行++; }}()); console.log(array);
.as-console-wrapper {max-height:100%!important;顶部: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如果(+ o.RowLocation!== row){o.RowLocation = row.toString();} row ++;}); console.log (array);
.as-console-wrapper {max-height:100%!important;顶部:0; }
I have a Json data that I need adjust before sending it to my component. My Json is as below. I need to identify the missing fields and move the below ones up.
[{
"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"
}]
My required Json is:
[{
"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"
}]
Here after (0,0), (0,1), property (0,2) is missing so I need to move it up and make it (0,2).. Same way after(1,0), property(1,1) is missing so it has to be (1,1).
I tried writing a custom function for this, but couldn't able to achieve it, so thought any map function that fits this scenario
I am getting gadgets information from the API. In some cases, some gadgets might be missing, so I need to pull there location up and draw the gadgets.
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) {
You could store the last column and row, then check if it is not the same column, then reset row counter.
Then check if RowLocation
is equal to row counter and if not set the new value.
Finally increment row counter.
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; }
You could use global variables instead if the closure, maybe this works for you.
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; }
这篇关于通过识别缺少的值来重新创建Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!