如何将JSON对象的所有空白值转换为连字符?

时间:2019-01-28 14:16:19

标签: javascript

这是我的样本回复:

var jsonData1 = [{
  firstName: "Sam",
  age: "10"
}, {
  firstName: "John",
  age: "11"
}, {
  firstName: "Jack",
  age: "12"
}, {
  firstName: "Pam",
  age: "13"
}, {
  firstName: "",
  age: "14"
}, {
  firstName: "Mitch",
  age: ""
}];

只要在任何字段中都有空白字符串的地方,我都希望将其显示为零。我想使用纯JS,没有lodash,没有jQuery。

8 个答案:

答案 0 :(得分:1)

您可以用一行代码编写它:

jsonData1.forEach(o => Object.keys(o).forEach(key => o[key] === '' && (o[key] = '-')))

答案 1 :(得分:1)

假设给定的数据结构,我会这样处理:

  • 使用Array.forEach()遍历数组中的元素
  • 使用Object.keys(person).forEach()遍历元素中的属性
  • 检查是否为空字符串并替换

赞:

jsonData1.forEach(person => {
     Object.keys(person).forEach(key => {
         if (person[key] === "") person[key] = "-";
     });
});

答案 2 :(得分:0)

jsonData1 = jsonData1.map(item=>{
    for(let key in item){
        if(item[key].length === 0) item[key] = 0;
    }
    return item;
})

具有简单循环

for(let i = 0; i<data.length;i++){
    let keys = Object.keys(data[i]); //returns an array of keys of object
    for(let key = 0;key<keys.length;key++){ 
        let keyName = keys[key];
        if(data[i][keyName].length === 0) data[i][keyName] = 0
    }
}

答案 3 :(得分:0)

有了数组后,就可以简单地对其进行迭代:

for (var i=0;i<jsonData1.length;i++) {
    if (jsonData1[i].age==="")
        jsonData1[i].age = 0;
    if (jsonData1[i].firstName==="")
        jsonData1[i].firstName = "Unknown";
}

答案 4 :(得分:0)

您可以使用for...of

  

for...of语句创建一个循环,循环访问可迭代对象,这些对象包括:内置字符串,数组,类似数组的对象(例如,参数或NodeList),TypedArray,Map,Set和用户定义的可迭代对象

var jsonData1 = [{
  firstName: "Sam",
  age: "10"}, {
  firstName: "John",
  age: "11"},{
  firstName: "Jack",
  age: "12"},{
  firstName: "Pam",
  age: "13"},{
  firstName: "",
  age: "14"},{
  firstName: "Mitch",
  age: ""
}];

for(var p of jsonData1){
  if(p.firstName.trim()=="") p.firstName="0";
  if(p.age.trim()=="") p.age="0"
}

console.log(jsonData1);

答案 5 :(得分:0)

尝试一下

 let newData = jsonData1.forEach(function(data) {
     for(ans in data) {
        If(data[ans] === "") data[ans] = 0;
     }
  });

答案 6 :(得分:0)

使用forEach循环

var jsonData1 = [
 {
firstName: "Sam",
age: "10"
  },
 {
firstName: "John",
age: "11"
  },
 {
firstName: "Jack",
age: "12"
  },
  {
firstName: "Pam",
age: "13"
  },
  {
firstName: "",
age: "14"
 },
 {
firstName: "Mitch",
age: ""
}
 ];
 jsonData1.forEach((e)=>{Object.keys(e).forEach((x)=>e[x]==""?e[x]=0:false)})
 console.log(jsonData1)

答案 7 :(得分:0)

这应该可以解决嵌套对象的问题:

const convertObj = obj => {
    Object.keys(obj).forEach(key => {
        if (obj[key] === '') {
            obj[key] = 0
        } else if (obj[key] instanceof Object) {
            convertObj(obj[key])
        }
    })

    return obj
}

样本测试:

const jsonData1 = [
   {
        firstName: "Mitch",
        age: "",
        nested_test: [
            {
                foo: 'bar',
                age: ''
            }
        ],
        nested_object: {
            foo: 'bar',
            age: ''
        }
    }
]

结果:

[
  {
    "firstName": "Mitch",
    "age": 0,
    "nested_array": [
      {
        "foo": "bar",
        "age": 0
      }
    ],
    "nested_object": {
      "foo": "bar",
      "age": 0
    }
  }
]