修改JSON数据

时间:2016-01-12 23:56:12

标签: javascript json

我有以下示例数据,我根据某些逻辑修改并返回修改后的对象

var obj = [{
  Primary: "Hejrwe",
  TestVal: "234723",
  TestTool: [{
    label: 'xyz',
    num: '23'
  }, {
    label: "",
    num: ""
  }, {
    label: "",
    num: ""
  }],
  TestItv: [{
    label: 'xyz',
    num: '23'
  }, {
    label: "",
    num: ""
  }, {
    label: "",
    num: ""
  }]
}, {
  Primary: "Urwhe",
  TestVal: "32432",
  TestTool: [{
    label: 'abc',
    num: '24'
  }, {
    label: "",
    num: ""
  }, {
    label: "",
    num: ""
  }],
  TestItv: [{
    label: 'abc',
    num: '24'
  }, {
    label: "",
    num: ""
  }, {
    label: "",
    num: ""
  }]
}];

我正在尝试删除TestTool数组中“label”和“num”键值为空字符串的任何键,目前我正在解决以下问题

var newObj = {};
Object.keys(obj).map(function(index) {
  var val = obj[index].TestTool;
  var temp = [];

  for (var i in val) {
    if (val[i].label !== "" || val[i].num !== "") {
      temp.push(val[i]);
      //delete val[i];
    }
  }

  obj[index].TestTool = temp;
  newObj = obj;
 });

 console.log(newObj); 

在上面的代码中,“newObj”返回修改后的对象,但如果查看应用的逻辑,我使用临时数组存储值,然后重新分配给“TestTool”键。

有没有更好的解决方案,我可以删除值,因为我通过对象?

3 个答案:

答案 0 :(得分:2)

根据您的标准,只需使用.filter()将集合缩减为新数组。

并且.map()应该替换为.forEach(),因为Object.keys()实际上是obj,所以Array不需要obj.forEach(function(val) { val.TestTool = val.TestTool.filter(function(o) { return o.label !== "" || o.num !== ""; }); }); 。< / p>

.filter()

因此,只要true回调返回var obj = [{ Primary: "Hejrwe", TestVal: "234723", TestTool: [{ label: 'xyz', num: '23' }, { label: "", num: "" }, { label: "", num: "" }], TestItv: [{ label: 'xyz', num: '23' }, { label: "", num: "" }, { label: "", num: "" }] }, { Primary: "Urwhe", TestVal: "32432", TestTool: [{ label: 'abc', num: '24' }, { label: "", num: "" }, { label: "", num: "" }], TestItv: [{ label: 'abc', num: '24' }, { label: "", num: "" }, { label: "", num: "" }] }]; obj.forEach(function(val) { val.TestTool = val.TestTool.filter(function(o) { return o.label !== "" || o.num !== ""; }); }); document.querySelector("pre").textContent = JSON.stringify(obj, null, 2);,该项就会被添加到最终返回的新数组中。

这是一个完整的演示:

&#13;
&#13;
<pre></pre>
&#13;
public int[] reverse(int[] arr) {
    for(int i = arr.length; i > 0 ; i--){
        System.out.print(arr[i-1] + " ");
    }
    return arr;
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这有效:

{{1}}

顺便说一句,在这里我假设您也希望过滤出 TestItv 空值。

如果标签和值都为空,我们还假设你想过滤

我还假设每个对象都有 TestTool TestItv ,否则您需要为 undefined 添加额外的检查。

答案 2 :(得分:0)

你可以尝试这样的事情。我只是在每个对象属性上使用delete运算符。

>     str(bwt.mu$model)
'data.frame':   189 obs. of  9 variables:
 $ low  : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
 $ age  : int  19 33 20 21 18 21 22 17 29 26 ...
 $ lwt  : int  182 155 105 108 107 124 118 103 123 113 ...
 $ race : Factor w/ 3 levels "white","black",..: 2 3 1 1 1 3 1 3 1 1 ...
 $ smoke: logi  FALSE FALSE TRUE TRUE TRUE FALSE ...
 $ ptd  : Factor w/ 2 levels "FALSE","TRUE": 1 1 1 1 1 1 1 1 1 1 ...
 $ ht   : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ ui   : logi  TRUE FALSE FALSE TRUE TRUE FALSE ...
 $ ftv  : Factor w/ 3 levels "0","1","2+": 1 3 2 3 1 1 2 2 2 1 ...
 - attr(*, "terms")=Classes 'terms', 'formula' length 3 low ~ age + lwt + race + smoke + ptd + ht + ui + ftv
  .. ..- attr(*, "variables")= language list(low, age, lwt, race, smoke, ptd, ht, ui, ftv)
  .. ..- attr(*, "factors")= int [1:9, 1:8] 0 1 0 0 0 0 0 0 0 0 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:9] "low" "age" "lwt" "race" ...
  .. .. .. ..$ : chr [1:8] "age" "lwt" "race" "smoke" ...
  .. ..- attr(*, "term.labels")= chr [1:8] "age" "lwt" "race" "smoke" ...
  .. ..- attr(*, "order")= int [1:8] 1 1 1 1 1 1 1 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(low, age, lwt, race, smoke, ptd, ht, ui, ftv)
  .. ..- attr(*, "dataClasses")= Named chr [1:9] "factor" "numeric" "numeric" "factor" ...
  .. .. ..- attr(*, "names")= chr [1:9] "low" "age" "lwt" "race" ...