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

时间:2017-06-09 20:03:04

标签: 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)。

我尝试为此编写自定义函数,但无法实现它,所以想到任何适合此场景的地图函数

我从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) {

2 个答案:

答案 0 :(得分:0)

import { Component, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Headers, Http, HttpModule ,Response } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-operationadd',
  templateUrl: './operationadd.component.html',
  styleUrls: ['./operationadd.component.css']
})
export class OperationaddComponent implements OnInit {

 name="abc";
  constructor(private http: Http) { }

  ngOnInit() {
  }

  submitdata(){
    console.log("Data Submitted");
    console.log(this.name);
var body = "name=" + this.name;
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');

 this.http.post('http://example.com/adv8/prod-api/crudtable-add.php', body, {
    headers: headers
    })
    .map(res => res.json())
    .subscribe(
     // data => this.saveJwt(data.id_token),
     // err => this.logError(err),
      () => console.log('Authentication Complete')
    );
}


}

答案 1 :(得分:0)

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

然后检查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; }