Angular js将字符串属性转换为json对象

时间:2018-05-31 13:25:15

标签: javascript

我正在使用角度js,我有以下包含许多json对象的数组,每个对象都有属性“isSingle”,其中包含“true”或“false”,我的问题是如何将此属性转换为boolean是或否:

[
    {
      "noFolder": "AW343",
      "type": "T7",
      "creationDate": "22/05/2017",
      "isSingle": "true"
    },
    {
      "noFolder": "AW35",
      "type": "T34",
      "creationDate": "09/05/2017",
      "isSingle": "false"
    },
    {
      "noFolder": "ASW3",
      "type": "T3",
      "creationDate": "05/07/2017",
      "isSingle": "true"
    },
    {
      "noFolder": "BG5",
      "type": "T1",
      "creationDate": "22/12/2018",
      "isSingle": "false"
    }

]

我想要的结果是拥有像这样的对象:

  {
      "noFolder": "ASW3",
      "type": "T3",
      "creationDate": "05/07/2017",
      "isSingle": true
    }

您是否知道如何将属性类型更改为bbolean。我正在使用angularjs ......

3 个答案:

答案 0 :(得分:4)

简单循环通过对象将字符串转换为bool:

yourArray.forEach(x => x.isSingle = x.isSingle === 'true');

答案 1 :(得分:1)

只需迭代数组并根据您的要求替换值 -

var obj = [
    {
      "noFolder": "AW343",
      "type": "T7",
      "creationDate": "22/05/2017",
      "isSingle": "true"
    },
    {
      "noFolder": "AW35",
      "type": "T34",
      "creationDate": "09/05/2017",
      "isSingle": "false"
    },
    {
      "noFolder": "ASW3",
      "type": "T3",
      "creationDate": "05/07/2017",
      "isSingle": "true"
    },
    {
      "noFolder": "BG5",
      "type": "T1",
      "creationDate": "22/12/2018",
      "isSingle": "false"
    }

]

obj.map((e) => {
    e.isSingle == "true" ? e.isSingle = true : e.isSingle = false
});

答案 2 :(得分:0)

使用reduce方法。

entity.HasMany(a => a.Contacts).WithOne(c => c.Address).OnDelete(DeleteBehavior.Cascade);