-5

Here is my sample response:

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

All I want is, wherever I have a blank string in any field, to show it as zero. I want to do it using plain JS, no lodash, no jQuery.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
tommy123
  • 587
  • 1
  • 10
  • 28

8 Answers8

1

You can write this in one line of code:

jsonData1.forEach(o => Object.keys(o).forEach(key => o[key] === '' && (o[key] = '-')))
idmean
  • 14,540
  • 9
  • 54
  • 83
1

Assuming the given data structure, I'd approach it like this:

  • loop through the elements in the array with Array.forEach()
  • loop through the properties in the element with Object.keys(person).forEach()
  • check for emptystring and replace

Like this:

jsonData1.forEach(person => {
     Object.keys(person).forEach(key => {
         if (person[key] === "") person[key] = "-";
     });
});
Duncan Thacker
  • 5,073
  • 1
  • 10
  • 20
0
jsonData1 = jsonData1.map(item=>{
    for(let key in item){
        if(item[key].length === 0) item[key] = 0;
    }
    return item;
})

With Simple Loop

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
    }
}
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

As you do have an array, you can simply iterate over it:

for (var i=0;i<jsonData1.length;i++) {
    if (jsonData1[i].age==="")
        jsonData1[i].age = 0;
    if (jsonData1[i].firstName==="")
        jsonData1[i].firstName = "Unknown";
}
Beppo
  • 176
  • 7
  • This doesn't cover the case for firstName – chazsolo Jan 28 '19 at 14:21
  • I'm afraid this won't work - jsonData[i].age.length is a number, so will never be equal to "". – Duncan Thacker Jan 28 '19 at 14:21
  • Looks better, although the `Unknown` seems to come out of nowhere! – Duncan Thacker Jan 28 '19 at 14:27
  • Of course you both are right, fixed, thanks! I got mixed uo when going from the ' .length===0 ' to the ' =="" ' approach. The firstName thing i missed because setting a name to zero didn't fit my mind ;-) - for the general approach, please refer to the other answers. – Beppo Jan 28 '19 at 14:29
0

You can use for...of

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, Array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables.

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);
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

Try this

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

Using forEach loop

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)
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

That should do the trick for nested objects:

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
}

Sample test:

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

Result:

[
  {
    "firstName": "Mitch",
    "age": 0,
    "nested_array": [
      {
        "foo": "bar",
        "age": 0
      }
    ],
    "nested_object": {
      "foo": "bar",
      "age": 0
    }
  }
]
Hammerbot
  • 15,696
  • 9
  • 61
  • 103